MySQL联合查询(SELECT … UNION)的使用
作用:将多个SELECT语句的结果合并到一个结果集中。
–数据表数据如下
±---------------±----------±-----------+
| person_id_card | brand | license |
±---------------±----------±-----------+
| ID000002 | 比亚迪 | 京A333901 |
| ID000002 | 宝马 | 京B55555 |
| ID000003 | 吉利 | 沪A568239 |
| ID000003 | 奥迪 | 沪C22222 |
| ID000005 | 大众 | 津A99999 |
| ID000001 | 长城 | 鲁A126985 |
| ID000001 | 奔驰 | 鲁A66666 |
±---------------±----------±-----------+
–第一种情况:两个表的字段名与查询的字段顺序完全相同。
select person_id_card, brand, license from car where person_id_card = ‘ID000001’
union
select person_id_card, brand, license from car where person_id_card = ‘ID000002’;
±---------------±----------±-----------+
| person_id_card | brand | license |
±---------------±----------±-----------+
| ID000001 | 长城 | 鲁A126985 |
| ID000001 | 奔驰 | 鲁A66666 |