gpt4 book ai didi

php - 如何使用多次出现的项目构建具有偏差的mysql查询

转载 作者:行者123 更新时间:2023-11-29 08:22:38 26 4
gpt4 key购买 nike

我想构建一个查询来随机显示 ITEM 表中的项目,但根据项目类别在 PREFERENCE 表中存在的次数(较大偏差)。类别必须自动添加到查询中。

ITEM TABLE
Itemname Category Id
'item1', '20081'
'item2 ', '15032'
'items3', '20081'
'item4', '20081'

PREFERENCE TABLE
Userid, Categoryname, Categoryid
'79', 'Everything Else', '15032'
'146', 'Antiques', '20081'
'79', 'Antiques', '20081'
'79', 'Antiques', '20081'
'79', 'Antiques', '20081'

简单来说就是这样

SELECT * FROM `ex`.`item` where category_id=20081 or category_id=79 /*there rest to be added automatically and also with the bias depending on the count in preference  */order by rand();

最佳答案

SELECT i.*
FROM item i
JOIN (SELECT Categoryid, COUNT(*) cat_count
FROM preference
WHERE Categoryid IN (20081, 79)
GROUP BY Categoryid) p
ON i.Categoryid = p.Categoryid
ORDER BY cat_count*RAND() DESC

关于php - 如何使用多次出现的项目构建具有偏差的mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18923015/

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