显示多个表中的列

时间:2019-03-13 11:08:55

标签: mysql sql

所以我有4个表,并且有一些重复的列,我想从每个表中选择列,并显示4个表中的每个列。

系统表

+------+-------+----------+-------+
| reg  |   ic  |    plate |  type |
+------+-------+----------+-------+
| 1000 | 22222 | WWW123   | car   |
| 1001 | 11111 | BBB987   | truck |
+------+-------+----------+-------+

所有者表

+------+-------+
| name |  ic   |
+------+-------+
| john |  2222 |
| joe  |  1111 |
+------+-------+

汽车桌

+--------+-------+------+-------+-------+----------+
| plate  | color | year | make  | model | capacity |
+--------+-------+------+-------+-------+----------+
| WWW123 | blue  | 2015 | Honda | City  |      1.5 |
+--------+-------+------+-------+-------+----------+

卡车桌子

+--------+-------+------+--------+-------+----------+---------+
| 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?

3 个答案:

答案 0 :(得分:0)

您可以在下面尝试-

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

答案 1 :(得分:0)

这是联接语句的一种情况。由于您要查找表之间的交点,因此我们将使用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

尝试执行它。您可能需要指定汽车和卡车的每个字段,而不要使用星号(*)

/ AF

答案 2 :(得分:0)

类似于其他答案。但是,你去了。

表结构和数据

 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查询

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)

输出

+-------+-------+--------+---------+--------+--------+-------+---------+--------+-----------+---------+
| 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    |
+-------+-------+--------+---------+--------+--------+-------+---------+--------+-----------+---------+

在线演示:http://sqlfiddle.com/#!9/52596e/3/0