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

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 0.74 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. sql double records in simple search
  2. SELECT ft.id, ft.title, ft.date
  3.   FROM forum_topics ft
  4.   WHERE ft.title LIKE '%" .$search. "%' OR ft.body LIKE '%" .$search. "%'
  5. UNION SELECT fr.topic, ft.title, fr.date
  6.   FROM forum_replies fr
  7.     INNER JOIN forum_topics ft ON fr.topic=ft.id
  8.   WHERE fr.body LIKE '%" .$search. "%'
  9. ORDER BY date DESC
  10.        
  11. SELECT g.id, g.title, g.date
  12. FROM (
  13.     SELECT ft.id, ft.title, ft.date
  14.     FROM forum_topics ft
  15.     WHERE ft.title LIKE '%" .$search. "%' OR ft.body LIKE '%" .$search. "%'
  16.     UNION
  17.     SELECT fr.topic as id, ft.title, fr.date
  18.     FROM forum_replies fr
  19.     INNER JOIN forum_topics ft ON fr.topic=ft.id
  20.     WHERE fr.body LIKE '%" .$search. "%'
  21. ) g
  22. GROUP BY g.id, g.title, g.date
  23. ORDER BY g.date DESC