[英]Display column from multiple tables
So I have 4 tables and there are some duplicate columns, and I want to select columns from each table and display every single column from the 4 tables. 所以我有4个表,并且有一些重复的列,我想从每个表中选择列并显示4个表中的每个列。
system table 系统表
+------+-------+----------+-------+
| reg | ic | plate | type |
+------+-------+----------+-------+
| 1000 | 22222 | WWW123 | car |
| 1001 | 11111 | BBB987 | truck |
+------+-------+----------+-------+
owner table 所有者表
+------+-------+
| name | ic |
+------+-------+
| john | 2222 |
| joe | 1111 |
+------+-------+
car table 车台
+--------+-------+------+-------+-------+----------+
| plate | color | year | make | model | capacity |
+--------+-------+------+-------+-------+----------+
| WWW123 | blue | 2015 | Honda | City | 1.5 |
+--------+-------+------+-------+-------+----------+
truck table 卡车桌
+--------+-------+------+--------+-------+----------+---------+
| plate | color | year | make | model | capacity | maxload |
+--------+-------+------+--------+-------+----------+---------+
| BBB987 | red | 2018 | Toyota | Hilux | 2.0 | 3000 |
+--------+-------+------+--------+-------+----------+---------+
Let's say I want to display every data in this way: 假设我想以这种方式显示每个数据:
+------+------+-------+--------+-------+------+--------+-------+----------+-------+---------+
| 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 |
+------+------+-------+--------+-------+------+--------+-------+----------+-------+---------+
Is there any possible way to generate SQL that does this? 有没有什么可能的方法来生成SQL呢?
You can try below - 您可以在下面尝试-
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
This is a case for join statements. 这是join语句的一种情况。 Since you want to find the intersection between the tables, we are going to use UNION ALL + an inner join (selecting values matching in multiple tables).
由于您要查找表之间的交点,因此我们将使用UNION ALL +内部联接(在多个表中选择匹配的值)。
SELECT name, ic, req, plate, year, color, make, model, capacity, maxload
FROM ((SELECT * FROM car c WHERE c.plate=plate) UNION ALL (SELECT * FROM truck AS t WHERE t.plate=plate))
INNER JOIN owner AS o ON o.ic=ic
Try to execute it. 尝试执行它。 You might need to specify every field from car and truck instead of using the asterix (*)
您可能需要指定汽车和卡车的每个字段,而不要使用星号(*)
/AF / AF
Similar to other answers. 与其他答案类似。 But here you go.
但是,你去了。
Table Structure & Data 表结构和数据
CREATE TABLE systemtbl
(
reg INT,
ic INT,
plate VARCHAR(255),
type VARCHAR(255)
);
CREATE TABLE ownertbl
(
name VARCHAR(255),
ic INT
);
CREATE TABLE cartbl
(
plate VARCHAR(255),
color VARCHAR(255),
year INT,
make VARCHAR(255),
model VARCHAR(255),
capacity DECIMAL(2, 1)
);
CREATE TABLE trucktbl
(
plate VARCHAR(255),
color VARCHAR(255),
year INT,
make VARCHAR(255),
model VARCHAR(255),
capacity DECIMAL(2, 1),
maxload INT
);
INSERT INTO systemtbl
VALUES (1000, 22222,'WWW123', 'car'),
(1001, 11111, 'BBB987', 'truck');
INSERT INTO ownertbl
VALUES ('john', 22222),
('joe', 11111);
INSERT INTO cartbl
VALUES ('WWW123', 'blue', 2015, 'Honda', 'City', 1.5);
INSERT INTO trucktbl
VALUES ('BBB987', 'red', 2018, 'Toyota', 'Hilux', 2.0, 3000 );
SQL Query SQL查询
SELECT systemtbl.reg,
ownertbl.name,
systemtbl.ic,
systemtbl.plate,
systemtbl.type,
tmp.color,
tmp.year,
tmp.make,
tmp.model,
tmp.capacity,
tmp.maxload
FROM (SELECT plate, color, year, make, model, capacity, NULL AS maxload
FROM cartbl
UNION ALL
SELECT plate, color, year, make, model, capacity, maxload
FROM trucktbl) tmp
INNER JOIN systemtbl USING (plate)
INNER JOIN ownertbl USING (ic)
Output 输出量
+-------+-------+--------+---------+--------+--------+-------+---------+--------+-----------+---------+
| reg | name | ic | plate | type | color | year | make | model | capacity | maxload |
+-------+-------+--------+---------+--------+--------+-------+---------+--------+-----------+---------+
| 1000 | john | 22222 | WWW123 | car | blue | 2015 | Honda | City | 1.5 | (null) |
| 1001 | joe | 11111 | BBB987 | truck | red | 2018 | Toyota | Hilux | 2 | 3000 |
+-------+-------+--------+---------+--------+--------+-------+---------+--------+-----------+---------+
Online Demo: http://sqlfiddle.com/#!9/52596e/3/0 在线演示: http : //sqlfiddle.com/#!9/52596e/3/0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.