赞
踩
UNION 语法:
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];参数
expression1, expression2, ... expression_n: 要检索的列。
tables: 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
ALL: 可选,返回所有结果集,包含重复数据。
一条数据拆分成多条
一条数据按照自定义字段type不同拆分为2条,使用union 操作符进行数据合并,并对合并的结果集使用排序、条件查询、分页
SELECT * FROM( (SELECT id as uId, name as uName, age as uAge, 1 as type FROM user) union all (SELECT id, name, age, 2 as type FROM user) ) con where uAge > 0 ORDER BY uAge desc limit 0, 10表数据:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。