Home > Database > Mysql Tutorial > MySQL UNION 中使用 LIMIT实例

MySQL UNION 中使用 LIMIT实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 09:58:12
Original
2201 people have browsed it

在 MySQL UNION 中使用 LIMIT 用于限制返回的记录条数,如果对 SELECT 子句做限制,需要对 SELECT 添加圆括号:

<code class="language-sql">(SELECT aid,title FROM article LIMIT 2) 
UNION ALL
(SELECT bid,title FROM blog LIMIT 2)</code>
Copy after login

该 SQL 会返回个 SELECT 语句的两条记录,如果不添加圆括号,则最后一个 LIMIT 2 会作用于整个 UNION 语句而一共返回 2 条记录。

同 ORDER BY 类似,当需要对整个 UNION 的结果进行 LIMIT 限制时,建议将各个 SELECT 语句用圆括号括起来以使语句更加清晰:

<code class="language-sql">(SELECT aid,title FROM article) 
UNION ALL
(SELECT bid,title FROM blog)
LIMIT 2</code>
Copy after login

可见,LIMIT 与 ORDER BY 经常搭配使用,二者在 UNION 中的使用方式也是一致的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template