mysql - 如何组合多个选择语句并限制它们中的每一个
问题描述
列cat
具有三个不同的值 - lorem
,ipsum
并且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
解决方案
所需的语法是将各个限制查询包装在括号中,如下所示
(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 括起来并将子句放在括号内...
推荐阅读
- python - Python - Scrapy splash 无法呈现此页面
- sql-server - Linux 上的 BCP 实用程序:NativeError = 18456 错误 = [unixODBC][Microsoft][ODBC Driver 17 for SQL Server][SQL Server]用户登录失败
- java - Firebase 服务帐户未读取
- coldfusion - 冷融合9:查询的查询显示没有结果
- sql - 如何更正 BAT 文件中的 Select Spool
- python - 使用逻辑运算符“not”的 While 循环说明
- c - 为什么修改 *int_one 的内存不会改变 *int_two 的内存值?
- python - 为什么我在列表理解 (Python) 中收到 NameError?
- node.js - 为给定主题的新消费者组设置初始“当前偏移量”和“滞后”
- android - 由于锁定画布错误,重新启动游戏活动时出错