Mysql查询
select t.text from topic t
inner join tag tg on tg.topic_id = t.id where tg.id in (3,4,5,6,7)
现在我如何从IN子句中匹配的最大值到最小值来订购这些结果。
更新 -
在那些建议添加order by tg.id desc
的人之间 - 请不要误解它的标签ID的个别值而不是IN子句中匹配的值的数量。
答案 0 :(得分:5)
您应该使用topic_id
表格中的tag
分组来加入您的表格,如下所示:
select t.text from topic t
inner join
(SELECT topic_id, COUNT(id) tags_count
FROM tag WHERE id IN (3,4,5,6,7)
GROUP BY topic_id
) tg on tg.topic_id = t.id
ORDER BY tg.tags_count DESC
答案 1 :(得分:-1)
试试这个
select t.text from topic t
inner join tag tg on tg.topic_id = t.id where tg.id in (3,4,5,6,7)
order by tg.id desc