mysql - 显示来自多个表的列

标签 mysql sql

所以我有 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/

相关文章:

PHP:想将变量和字符串放入mysql中

sql - postgreSQL:插入多行,其中只有一列值发生变化

sql - UPPER() 不适用于 PostgreSQL 8.2 数据库中的西里尔符号

php - 在 php 脚本中检查特定的 mysql 连接是否已经存在?

php - 错误号 : 1292 Truncated incorrect DOUBLE value in my SQL error

Mysql 根据条件连接表

sql - 如何从另一个表中获取引用值?

sql - 导致具有重复日期的窗口函数

mysql - PostgreSQL:从文件系统加载文件而不插入表

mysql - 按文本和数字排序