gpt4 book ai didi

mysql - 出现次数的订单记录 "in array"

转载 作者:行者123 更新时间:2023-11-29 07:05:51 25 4
gpt4 key购买 nike

基本上我想要

Select * from x 
JOIN y
on x.id = y.x_id
where y.tag_id in (1, 2, 3, 4) order by [the amount of hits of y.tag_id]

所以

假设 y 可以是:

id | x_id | tag_id
1 | 55 | 2
2 | 55 | 3
3 | 66 | 1

我想让记录 x.id = 55 在 x.id = 66 之上排序。55 有 2 个命中(2 和 3),而 66 只有 1 个命中

最佳答案

您可以在子查询中查找每个 x_id 的命中计数,将其与 X 表连接并按计算的计数对结果进行排序。

Select * from x 
JOIN y
on x.id = y.x_id
inner join (
select x_id, count(*) cnt
from y
group x_id
) t on x.id = t.x_id
where y.tag_id in (1, 2, 3, 4)
order by t.cnt desc;

关于mysql - 出现次数的订单记录 "in array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com