首页 > 解决方案 > 如何组合多个选择语句并限制它们中的每一个

问题描述

cat具有三个不同的值 - loremipsum并且dolor
需要15 rows从每个类别中进行选择 ( cat) 即45 rows total
尝试使用union- 没有成功
有任何帮助吗?

select id, title from arts where cat = 'lorem' order by ind asc limit 15
union
select id, title from arts where cat = 'ipsum' order by ind asc limit 15
union
select id, title from arts where cat = 'dolor' order by ind asc limit 15

标签: mysqlsqlsql-order-byunionwindow-functions

解决方案


所需的语法是将各个限制查询包装在括号中,如下所示

(select id, title from arts where cat = 'lorem' order by ind asc limit 15)
union
(select id, title from arts where cat = 'ipsum' order by ind asc limit 15)
union
(select id, title from arts where cat = 'dolor' order by ind asc limit 15)

文档中:

要将 ORDER BY 或 LIMIT 子句应用于单个 SELECT [in a union],请将 SELECT 括起来并将子句放在括号内...


推荐阅读