Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 0.74 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get user Posts and/or Comments in a sharing blog using SQL
  2. SELECT p.id AS post_id, p.author_id AS post_author_id, c.author_id AS comment_author_id, title, c.content
  3.  
  4. FROM Posts p JOIN Comments c ON p.id = c.post_id
  5.  
  6. WHERE p.author_id = $userId
  7.  
  8. OR c.author_id = $userId
  9.        
  10. SELECT p.id AS post_id,
  11.        p.author_id AS post_author_id,
  12.        c.author_id AS comment_author_id,
  13.        p.title,
  14.        c.content
  15. FROM Posts p
  16. LEFT JOIN Comments c ON p.id = c.post_id AND c.author_id = $userId
  17. WHERE p.author_id = $userId
  18. UNION ALL
  19. SELECT p.id AS post_id,
  20.        p.author_id AS post_author_id,
  21.        c.author_id AS comment_author_id,
  22.        p.title,
  23.        c.content
  24. FROM Posts p
  25. RIGHT JOIN Comments c ON p.id = c.post_id
  26. WHERE c.author_id = $userId