sql - MySQL fetch data from another table where the column row matches a row result from first table -


i have 2 tables, let's contain:

table1

id | username  1  | bla 4  | bla2 

table2

from_id | fk_toid | action  1       | 2       | -1 4       | 2       | -1 

now have like:

select fk_fromid table2 fk_toid = 2 , fp_action = -1 

what want fetch id , username table1, id of table1 matches from_id table2, fk_toid '2' in table2.

so results returned should

[{ id: 1, fk_fromid: 1, username: bla }, { id: 4, fk_fromid: 4, username: bla2 }]

you need this:

select a.id,b.from_id fk_fromid,a.username t1 left join t2 b on a.id=b.from_id 

click link see result:

http://sqlfiddle.com/#!2/868c1/4

update:1

    select a.id,b.from_id fk_fromid,a.username     t1     left join t2 b     on a.id=b.from_id      b.fk_toid=2 , b.action=-1; 

check link:

http://sqlfiddle.com/#!2/868c1/8


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -