mysql中union的用法

 

表A

mysql> select * from info_a;
+------+--------+--------+--------+
| num  | uid    | beizhu | stk    |
+------+--------+--------+--------+
|   10 | henry1 | test1  | 000001 |
|   12 | henry1 | test2  | 000002 |
|   13 | Henry2 | test3  | 000003 |
|   10 | henry3 | test11 | 000006 |
|   10 | henry3 | test22 | 000007 |
|   12 | henry4 | test33 | 000008 |
|   12 | henry4 | test44 | 000009 |
|   13 | Henry5 | test55 | 000010 |
+------+--------+--------+--------+
8 rows in set (0.00 sec)

 

表B:

mysql> select * from info_b;
+------+------+----------+--------+
| buy  | sell | uid      | stk    |
+------+------+----------+--------+
|  100 |  100 | henry1   | 000001 |
|  200 |  100 | henry1   | 000002 |
|  300 |  100 | henry2   | 000003 |
|  400 |  100 | henry3   | 000006 |
|  500 |  100 | Henry100 | 000100 |
|  600 |  100 | Henry101 | 000101 |
|  700 |  100 | Henry102 | 000102 |
|  701 |  100 | Henry102 | 000103 |
+------+------+----------+--------+
8 rows in set (0.00 sec)

 

 

 

## union 查询
select
uid, stk from info_a union select uid,stk from info_b ;

 

+----------+--------+
| uid      | stk    |
+----------+--------+
| henry1   | 000001 |
| henry1   | 000002 |
| Henry2   | 000003 |
| henry3   | 000006 |
| henry3   | 000007 |
| henry4   | 000008 |
| henry4   | 000009 |
| Henry5   | 000010 |
| Henry100 | 000100 |
| Henry101 | 000101 |
| Henry102 | 000102 |
| Henry102 | 000103 |
+----------+--------+
12 rows in set (0.00 sec)

 

union 查询, 将 【表A】 的查询结果和 【表B】 的查询结果进行合并,去重;

 

posted @ 2023-03-03 16:26  He_LiangLiang  阅读(41)  评论(0编辑  收藏  举报