码迷,mamicode.com
首页 > 数据库 > 详细

sql 基础--mysql 5 (8)

时间:2015-02-28 11:22:30      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

15.组合查询 union

mysql> select uid,first_name,price from pw_price where uid >2 union select uid,first_name,price from
 pw_price where price>=1000;
+-----+------------+-------+
| uid | first_name | price |
+-----+------------+-------+
|   3 | li         |  2000 |
|   2 | zhang      |  1000 |
+-----+------------+-------+
2 rows in set (0.00 sec)

mysql> select uid,first_name,price from pw_price where uid >2 or price>=1000;
+-----+------------+-------+
| uid | first_name | price |
+-----+------------+-------+
|   2 | zhang      |  1000 |
|   3 | li         |  2000 |
+-----+------------+-------+
2 rows in set (0.00 sec)

不去重显示所有结果

mysql> select uid,first_name,price from pw_price where uid >2 union all select uid,first_name,pri
from pw_price where price>=1000;
+-----+------------+-------+
| uid | first_name | price |
+-----+------------+-------+
|   3 | li         |  2000 |
|   2 | zhang      |  1000 |
|   3 | li         |  2000 |
+-----+------------+-------+
3 rows in set (0.00 sec)

组合结果排序 放在最后的select语句中

mysql> select uid,first_name,price from pw_price where uid >2 union select uid,first_name,price from
 pw_price where price>=1000 order by uid desc;
+-----+------------+-------+
| uid | first_name | price |
+-----+------------+-------+
|   3 | li         |  2000 |
|   2 | zhang      |  1000 |
+-----+------------+-------+
2 rows in set (0.00 sec)

 

sql 基础--mysql 5 (8)

标签:

原文地址:http://www.cnblogs.com/wjw334/p/4304984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!