TableA
id name
1 John
2 Jane
3 Joe
TableB
id uid file bg avatar
1 2 path_to_photo 0 0
2 2 path_to_photo 1 0
3 2 path_to_photo 0 1
4 1 path_to_photo 1 1
Assumed I picked Jane from TableA, I want to display the image which she set as background and which one she set as avatar altogether like in facebook profile.
How do I do that.
Note that all is set to display in single block. Can I do it in one query.
SELECT
TableA.id, TableA.name, TableB.uid, TableB.file, TableB.bg, TableB.avatar
FROM
TableA
RIGHT JOIN
TableB
ON
TableA.id = TableB.uid AND (TableB.bg = '1' OR TableB.avatar ='1')
WHERE
TableA.id = :user_id
GROUP BY TableA.id
molona, you also need file in the SELECT, otherwise he won’t be able to “display the image which she set as background and which one she set as avatar”
plus, you have to filter out the pics that aren’t either background or avatar
SELECT a.id
, a.name
, b.file
, b.bg
, b.avatar
FROM TableA as a
INNER
JOIN TableB as b
ON b.uid = a.id
AND 1 IN ( b.bg , b.avatar )
WHERE a.id = 2
I guess I should have added it because he did in his query… but his question didn’t say anything about getting a result only when one or the other where greater than 0… so I decided to be a little bit lazy and that he could add it himself, if he really wanted