
Untitled
By: a guest on
May 6th, 2012 | syntax:
None | size: 0.74 KB | hits: 12 | expires: Never
sql double records in simple search
SELECT ft.id, ft.title, ft.date
FROM forum_topics ft
WHERE ft.title LIKE '%" .$search. "%' OR ft.body LIKE '%" .$search. "%'
UNION SELECT fr.topic, ft.title, fr.date
FROM forum_replies fr
INNER JOIN forum_topics ft ON fr.topic=ft.id
WHERE fr.body LIKE '%" .$search. "%'
ORDER BY date DESC
SELECT g.id, g.title, g.date
FROM (
SELECT ft.id, ft.title, ft.date
FROM forum_topics ft
WHERE ft.title LIKE '%" .$search. "%' OR ft.body LIKE '%" .$search. "%'
UNION
SELECT fr.topic as id, ft.title, fr.date
FROM forum_replies fr
INNER JOIN forum_topics ft ON fr.topic=ft.id
WHERE fr.body LIKE '%" .$search. "%'
) g
GROUP BY g.id, g.title, g.date
ORDER BY g.date DESC