sql 基础--mysql 5 (8)

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)

 

posted on 2015-02-28 11:04  wjw334  阅读(107)  评论(0编辑  收藏  举报

导航