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)