在两个表中搜索并按术语的出现顺序排序
我有这个查询要在两个 SQL 表中搜索。我正在寻找一种按出现次数对结果进行排序的方法。这是我的查询:
SELECT `parent_id`
FROM wp_forum_posts
WHERE text LIKE '%{$term1}%'
UNION
SELECT `id`
FROM wp_forum_threads
WHERE subject LIKE '%{$term1}%
获得排序结果的最佳方法是什么?
I have this query to search in two SQL tables. I am looking for a way to sort the result by occurrence. This is my query:
SELECT `parent_id`
FROM wp_forum_posts
WHERE text LIKE '%{$term1}%'
UNION
SELECT `id`
FROM wp_forum_threads
WHERE subject LIKE '%{$term1}%
Which is the best way, to get the results ordered?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诀窍是首先使用
UNION ALL
,它保留重复项(普通UNION
删除重复项),然后从结果中进行选择。这个查询应该这样做:The trick is first to use
UNION ALL
, which preserves duplicates (ordinaryUNION
removes duplicates), then select from that result. This query should do it:假设 ID 和 Parent_ID 在表中不重复,否则您可以为每个 id 获取 2 行...如果是这样,您是否希望将它们加在一起,那么 Parent_ID 和 ID 相关吗?
Assumes ID and parent_ID are not duplicates in tables otherwise you can get 2 rows per an id... and would you want them summed together if so then are parent_ID and ID related?