gpt4 book ai didi

mysql - 返回按同一列上的总匹配项排序的结果

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:41 25 4
gpt4 key购买 nike

我有下表:

+---------+------------+
| EntryID | CategoryID |
+---------+------------+
| 11 | 15 |
| 11 | 18 |
| 186 | 15 |
| 186 | 18 |
| 186 | 334 |
| 187 | 15 |
| 187 | 18 |
| 187 | 337 |
| 278 | 15 |
| 278 | 18 |
| 278 | 337 |
| 278 | 457 |
+---------+------------+

我想按照匹配的相关性顺序获得结果,即指定的 CategoryID 行/CategoryIDs 的百分比

我坚持的地方是这个计算需要对多个 categoryID 查询起作用。例如,如果我要查找 categoryID 18,entryID 11 应该排在第一位,因为它的匹配百分比(请原谅我的陈词滥调)为 50%,然后186 或 187 以 33% 的匹配百分比排在第二位(在我的用例中,排序无关紧要),然后 278 以 25% 的匹配百分比排在最后。

我的问题是:有没有办法在一个/多个 SQL 查询/查询中进行这种排序?手头有 Java,所以我可以用 Java 而不是 MySQL 进行排序,但我想看看是否有纯 SQL 解决方案。此外,是否有更好的方法来计算我的案例的相关性?

(只是为了好玩,这个问题有更好的标题吗?)

最佳答案

听起来你想要这样的东西:

select EntryID, count(*)
from your_table
group by EntryID
order by count(*) asc

更新:根据更新后的问题,这里有一种方法可以实现:

select your_table.EntryID, count(*) as matches, sub_query.total_rows
from your_table
inner join (
select EntryID, count(*) as total_rows
from your_table
group by EntryID
) sub_query on sub_query.EntryID = your_table.EntryID
where your_table.CategoryID = 18
group by your_table.EntryID
order by (count(*) / sub_query.total_rows) desc

关于mysql - 返回按同一列上的总匹配项排序的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21007257/

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