时间:2021-07-01 10:21:17 帮助过:19人阅读
主要介绍的是MySQLunion 语法代码演示,我前两天在相关网站看见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语法的介绍,望你能有所收获。