Help with grasping (INNER?) JOIN
Posted
by Greenie
on Stack Overflow
See other posts from Stack Overflow
or by Greenie
Published on 2010-03-23T23:07:28Z
Indexed on
2010/03/23
23:13 UTC
Read the original article
Hit count: 288
I'm having trouble building a query. I can do what I want in 3 different queries.
SELECT id FROM table1 WHERE url LIKE '%/$downloadfile'
put that in $url_id
SELECT item_id FROM table2 WHERE rel_id = '$url_id'"
put that in $item_id
SELECT rel_id FROM table2 WHERE rel_id = '$item_id' AND field_id = '42'"
put that in $user_id
But from reading examples on joins and inner joins I think there's a more elegant way. I cant wrap my brain around writing a better query (but would like to) I can describe how it should go:
table1 fields: id, url
table2 fields item_id, rel_id, field_id
I know the last part of table1.url (LIKE '%/$filename') with that I select table1.id. table1.id is equal to one entry in table2.rel_id. So get that and select the table2.item_id. In table2 there is another entry which has the same table2.item_id and it will have a table2.field_id = '42' And finally the value I need is the table2.rel_id where the table2.field_id was 42. I will fetch that value and put it in $user_id
Can this be done with one query using joins/inner joins?
© Stack Overflow or respective owner