需要帮助将此sql查询简化为单选:
(SELECT * FROM `deals`
WHERE category_id = 1
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 2
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 4
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 5
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 6
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 8
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 9
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 10
ORDER BY id desc
LIMIT 10)
UNION
(SELECT * FROM `deals`
WHERE category_id = 17
ORDER BY id desc
LIMIT 10)
有人告诉我,要试着用“分组”和“有”。然而,我尝试的任何查询都没有任何效果。。。
任何帮助都将不胜感激!
编辑-抱歉,忘记提到数据库引擎是mysql
3条答案
按热度按时间4xrmg8kj1#
对于没有窗口功能的老版本mysql,下面是代码。
您需要将必要的字段名添加到另一个select。
这使用了这里描述的技术
bz4sfanl2#
我不确定,我需要知道你是否需要10的限制,这是不是像拿所有这些东西的前10?
如果没有,那么
o75abkj43#
您可以使用一个窗口函数将其压缩,将每个组bucket限制为10个。