我有2個表格主題和帖子,我想搜索我的帖子,並按大多數匹配的線程順序。MYSQL,ORDER BY LIKE
現在,我有這樣的:
SELECT Threads.*
FROM Posts
INNER JOIN Threads ON Posts.ThreadID=Threads.ThreadID
WHERE Posts.Content LIKE '%" . $search . "%'
OR Posts.User LIKE '%" . $search ."%'
我有2個表格主題和帖子,我想搜索我的帖子,並按大多數匹配的線程順序。MYSQL,ORDER BY LIKE
現在,我有這樣的:
SELECT Threads.*
FROM Posts
INNER JOIN Threads ON Posts.ThreadID=Threads.ThreadID
WHERE Posts.Content LIKE '%" . $search . "%'
OR Posts.User LIKE '%" . $search ."%'
你可以添加一個連接來計算每個線程的職位數目。由於您沒有從帖子中選擇任何欄目,這是您需要的唯一連接。
select t.*
from Threads t
join (
select ThreadID
, count(*) as PostCount
from Posts
where p.Content LIKE '%" . $search . "%'
or p.User LIKE '%" . $search ."%'
group by
ThreadID
) PostCount
on PostCount.ThreadID = t.ThreadID
order by
PostCount.PostCount desc
你忘了問一個問題:) – 2012-02-19 21:17:34
我相信問題是在主題行中:你將如何使用LIKE添加一個ORDER BY? – DOK 2012-02-19 21:19:19
@JoachimIsaksson:問題是在第一行,「我想搜索我的帖子,並按大多數匹配的線程排序」 – Andomar 2012-02-19 21:22:22