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

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 0.52 KB  |  hits: 12  |  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. MySQL Order query response by number of matches on another row
  2. SELECT b.id, COUNT(b.id) AS count
  3. FROM (`users_colleges` AS a)
  4. RIGHT JOIN `colleges` AS b ON `b`.`id` = `a`.`college_id`
  5. GROUP BY `b`.`id`
  6. ORDER BY COUNT(b.id) DESC
  7.        
  8. SELECT c.id, COUNT(uc.college_id) AS count
  9. FROM colleges AS c
  10. LEFT JOIN users_colleges AS uc
  11. ON c.id = uc.college_id
  12. GROUP BY c.id
  13. ORDER BY COUNT(uc.college_id) DESC
  14.        
  15. WHERE NOT EXISTS
  16. (
  17.     SELECT *
  18.     FROM users_colleges AS uc2
  19.     WHERE uc2.college_id = c.id
  20.     AND uc2.user_id = '42'
  21. )