I’m creating a social network and I have a pretty specific case, and I’ve almost got the correct query. I have 6 tables that I’m merging into one query.
Table recent is used to find the exact rows on tables brostplain, egobrost, and brostpublic, it gets complicated when also finding a match on members and egos tables, where members.username will be matched to brostplain and brostpublic on column id, and egos.egoname will be matched to egobrost on column egoid. All of it is sorted by unix timestamp, in the recent.realtime column. Here’s the current query:
SELECT recent.id,
recent.brosttype,
recent.postid,
recent.realtime,
members.username,
egos.egoname,
brostplain.id,
brostplain.postid,
brostplain.content,
brostplain.up,
brostplain.down,
brostplain.timedata,
brostplain.realtime,
brostplain.attached,
brostplain.replies,
brostplain.ext,
brostplain.onpage,
brostplain.touser,
brostplain.isego,
brostplain.category,
egobrost.egoid,
egobrost.postid,
egobrost.content,
egobrost.up,
egobrost.down,
egobrost.timedata,
egobrost.realtime,
egobrost.attached,
egobrost.replies,
egobrost.ext,
egobrost.onpage,
egobrost.touser,
egobrost.isego,
egobrost.category,
brostpublic.id,
brostpublic.postid,
brostpublic.content,
brostpublic.up,
brostpublic.down,
brostpublic.timedata,
brostpublic.realtime,
brostpublic.attached,
brostpublic.replies,
brostpublic.ext,
brostpublic.category,
brostpublic.isego
FROM recent
LEFT JOIN brostplain ON recent.brosttype=brostplain.brosttype
AND recent.postid=brostplain.postid
LEFT JOIN brostpublic ON recent.brosttype=brostpublic.brosttype
AND recent.postid=brostpublic.postid
LEFT JOIN egobrost ON recent.brosttype=egobrost.brosttype
AND recent.postid=egobrost.postid
LEFT JOIN egos ON egos.egoid=egobrost.egoid
JOIN members ON brostplain.id=members.id OR brostpublic.id=members.id
WHERE recent.id='2' ORDER BY recent.realtime DESC
Now this gets all the relevant rows, but it doesn’t return any results for egobrost rows due to the way egos table is joined, and I can’t figure out where to put the join or how to implement it. If I omit the JOIN on egos and remove egos.egoname from my SELECT, it will show the rows on egobrost table no problem, but I need the egoname for my output. Any help would be greatly appreciated.
EDIT (Solved): After much thought I solved this by adding LEFT to the JOIN on members. Now the result set is complete. Thanks to those who contributed.
Resolved by changing the JOIN on members to a LEFT JOIN, coinciding with the LEFT JOIN on egos. Here is the final query: