主要介绍的是MySQLunion 语法代码演示,我前两天在相关网站看见MySQLunion 语法代码演示的资料,觉得挺好,就拿出来供大家分享。希望在你今后的学习中会对你有所帮助。
代码如下:
SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
SELECT ... UNION [ALL | DISTINCT] SELECT ... [UNION [ALL | DISTINCT] SELECT ...]
UNION 用于把来自许多SELECT语句的结果组合到一个结果集合中。 (如果你要将多个表的查询结果进行合并
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;
To apply ORDER BY or LIMIT to an individual SELECT,
place the clause inside the parentheses that enclose the SELECT:
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;
To apply ORDER BY or LIMIT to an individual SELECT,
place the clause inside the parentheses that enclose the SELECT:
为了对单个SELECT使用ORDER BY或LIMIT,应把子句放入圆括号中。圆括号包含了SELECT:
代码
代码如下:
(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
二 实例扩展
MySQLunion可以对同一个表的两次查询联合起来. 这样做的益处也非常明显, 比如在blog应用中, 可以利用一条sql语句实现置顶blog和普通blog的分页显示.
代码
代码如下:
(
SELECT *
FROM `blog`
WHERE top=1
ORDER BY created DESC
)
UNION (
SELECT *
FROM `blog`
WHERE top = 0
ORDER BY created DESC
) LIMIT 2 , 3
以上的相关内容就是对MySQLunion语法的介绍,望你能有所收获。声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com