所以我有 4 个表并且有一些重复的列,我想从每个表中选择列并显示 4 个表中的每一列。
系统表
+------+-------+----------+-------+
| reg | ic | plate | type |
+------+-------+----------+-------+
| 1000 | 22222 | WWW123 | car |
| 1001 | 11111 | BBB987 | truck |
+------+-------+----------+-------+
所有者表
+------+-------+
| name | ic |
+------+-------+
| john | 2222 |
| joe | 1111 |
+------+-------+
车 table
+--------+-------+------+-------+-------+----------+
| plate | color | year | make | model | capacity |
+--------+-------+------+-------+-------+----------+
| WWW123 | blue | 2015 | Honda | City | 1.5 |
+--------+-------+------+-------+-------+----------+
卡车 table
+--------+-------+------+--------+-------+----------+---------+
| plate | color | year | make | model | capacity | maxload |
+--------+-------+------+--------+-------+----------+---------+
| BBB987 | red | 2018 | Toyota | Hilux | 2.0 | 3000 |
+--------+-------+------+--------+-------+----------+---------+
假设我想以这种方式显示所有数据:
+------+------+-------+--------+-------+------+--------+-------+----------+-------+---------+
| reg | name | ic | plate | color | year | make | model | capacity | type | maxload |
+------+------+-------+--------+-------+------+--------+-------+----------+-------+---------+
| 1000 | John | 22222 | WWW123 | blue | 2015 | Honda | City | 1.5 | car | |
| 1001 | Joe | 11111 | BBB987 | red | 2018 | Toyota | Hilux | 2.0 | truck | 3000 |
+------+------+-------+--------+-------+------+--------+-------+----------+-------+---------+
是否有任何可能的方法来生成执行此操作的 SQL?
最佳答案
你可以试试下面-
select reg,name,A.ic,A.plate,color,year,make,model,capacity,type,maxload
from systemtable A
inner join ownertable B on A.ic=B.ic
join
(
select plate, color ,year ,make ,model, capacity,null as maxload from car
union all
select plate, color ,year ,make ,model, capacity,maxload from truck
)C on A.plate=C.plate
关于mysql - 显示来自多个表的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55140470/