I have tried googling but couldn't get any information to echo a record from specific table while I generate the query using JOIN.
Here's my code:
SELECT * FROM tbl1 INNER JOIN tbl2 ON tbl1.id = tbl2.id
I want to get the id
from tbl1
. When I used,
echo $row->id
It does echo the id
column from tbl2
. What should I do to get the record from the table which I want? Thanks!
You need to use alias because in both tables you have same column name id, so you can use like that:
SELECT tbl1.id as id1,tbl2.id as id2
FROM tbl1
INNER JOIN tbl2 ON tbl1.id = tbl2.id
And:
echo $row->id1 // for table 1
echo $row->id2 // for table 2
Because 2 table tbl1
and tbl2
both have same column named id
and MySQL don't separate them and return id
field of last table. So you need add alias name for id
field.
SELECT *, tbl1.id as id_tbl1 FROM tbl1 INNER JOIN tbl2 ON tbl1.id = tbl2.id
try this
SELECT tbl1.id as tb1id,tbl2.id as tbl2id FROM tbl1 INNER JOIN tbl2
ON tbl1.id = tbl2.id