
Untitled
By: a guest on
Apr 24th, 2012 | syntax:
None | size: 0.74 KB | hits: 13 | expires: Never
Get user Posts and/or Comments in a sharing blog using SQL
SELECT p.id AS post_id, p.author_id AS post_author_id, c.author_id AS comment_author_id, title, c.content
FROM Posts p JOIN Comments c ON p.id = c.post_id
WHERE p.author_id = $userId
OR c.author_id = $userId
SELECT p.id AS post_id,
p.author_id AS post_author_id,
c.author_id AS comment_author_id,
p.title,
c.content
FROM Posts p
LEFT JOIN Comments c ON p.id = c.post_id AND c.author_id = $userId
WHERE p.author_id = $userId
UNION ALL
SELECT p.id AS post_id,
p.author_id AS post_author_id,
c.author_id AS comment_author_id,
p.title,
c.content
FROM Posts p
RIGHT JOIN Comments c ON p.id = c.post_id
WHERE c.author_id = $userId