如何用规则加入 mysql 中的 3 table?
How to join 3 table in mysql with rule?
首先,我有3个table。
Table 1条数据:
tid | type_1 | address_1 | contact_1
----+--------+-----------+-----------+
1 | 4 | No.2123 | 01234567
4 | 4 | No.4567 | 00011234
Table 2条数据:
tid | type_2 | address_2 | contact_2
----+--------+-----------+-----------+
2 | 3 | No.8888 | 7654321
Table3条数据:
tid | subject | desc
----+---------+-------------+
1 | Test 1 | Desc 1
2 | Test 2 | Desc 2
3 | Test 3 | Desc 3
4 | Test 4 | Desc 4
我想这样组合:
tid | subject | type_1 | type_2 | address_1 | address_2 | contact_1 | contact_2 | desc
----+---------+--------+--------+-----------+-----------+-----------+-----------+------
1 | Test 1 | 4 | | No.2123 | |01234567 | | Desc1
2 | Test 2 | | 3 | |No.8888 | |7654321 | Desc2
4 | Test 4 | 4 | | No.4567 | |00011234 | | Desc4
如果 table 1 得到 "tid" 1,那么 table 2 永远不会得到相同的时间。
如果在 table 1 和 table 2 中找不到 tid,则忽略
谢谢!
我之前的代码是:
$query = mysql_query("SELECT table1.*, table2.*, table3.subject, table3.desc, FROM table1 ON table1.tid = table3.tid LEFT JOIN table2 ON table2.tid = table3.tid LEFT JOIN table3 ON table1.tid AND table2.tid = table3.tid");
您所写的JOIN
子句格式不正确。你应该这样使用它:
`table1 JOIN table2 ON` your conditions to join the table.
您可以从 here 获得更多关于 JOIN
的信息。
所以你可以试试这个:
SELECT table3.tid AS tid,
t3.subject AS subject,
t1.type_1 AS type_1,
t2.type_2 AS type_2,
t1.address_1 AS address_1,
t2.address_2 AS address_2,
t1.contact_1 AS contact_1,
t2.contact_2 AS contact_2,
t3.desc AS description
FROM table3 t3
INNER JOIN table1 t1 ON t3.tid = t1.tid
INNER JOIN table2 t2 ON t3.tid = t2.tid
SELECT
table1.tid,
table3.subject,
table1.type_1,
NULL AS type_2,
table1.address_1,
NULL AS address_2,
table1.contact_1,
NULL AS contact_2,
table3.`desc`
FROM table1
INNER JOIN table3 ON table1.tid=table3.tid
UNION ALL
SELECT
table2.tid,
table3.subject,
NULL AS type_1,
table2.type_2,
NULL AS address_1,
table2.address_2,
NULL AS contact_1,
table2.contact_2,
table3.`desc`
FROM table2
INNER JOIN table3 ON table2.tid=table3.tid
DROP TABLE IF EXISTS table1;
CREATE TABLE table1
(tid INT NOT NULL
,type_1 INT NOT NULL
,address_1 VARCHAR(12) NOT NULL
,contact_1 VARCHAR(12) NOT NULL
);
INSERT INTO table1 VALUES
(1 ,4 ,'No.2123','01234567'),
(4 ,4 ,'No.4567','00011234');
DROP TABLE IF EXISTS table2;
CREATE TABLE table2
(tid INT NOT NULL
,type_2 INT NOT NULL
,address_2 VARCHAR(12) NOT NULL
,contact_2 VARCHAR(12) NOT NULL
);
INSERT INTO table2 VALUES
(2 ,3 ,'No.8888','7654321');
DROP TABLE IF EXISTS table3;
CREATE TABLE table3
(tid INT NOT NULL
,subject VARCHAR(12) NOT NULL
,description VARCHAR(12) NOT NULL
);
INSERT INTO table3 VALUES
(1 ,'Test 1','Desc 1'),
(2 ,'Test 2','Desc 2'),
(3 ,'Test 3','Desc 3'),
(4 ,'Test 4','Desc 4');
SELECT x.*
, y.subject
, y.description
FROM
( SELECT tid
, type_1
, NULL type_2
, address_1
, contact_1
, NULL address_2
, NULL contact_2
FROM table1
UNION
SELECT tid
, NULL
, type_2
, NULL
, NULL
, address_2
, contact_2
FROM table2
) x
JOIN table3 y
ON y.tid = x.tid;
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| tid | type_1 | type_2 | address_1 | contact_1 | address_2 | contact_2 | subject | description |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| 1 | 4 | NULL | No.2123 | 01234567 | NULL | NULL | Test 1 | Desc 1 |
| 2 | NULL | 3 | NULL | NULL | No.8888 | 7654321 | Test 2 | Desc 2 |
| 4 | 4 | NULL | No.4567 | 00011234 | NULL | NULL | Test 4 | Desc 4 |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
3 rows in set (0.00 sec)
首先,我有3个table。
Table 1条数据:
tid | type_1 | address_1 | contact_1
----+--------+-----------+-----------+
1 | 4 | No.2123 | 01234567
4 | 4 | No.4567 | 00011234
Table 2条数据:
tid | type_2 | address_2 | contact_2
----+--------+-----------+-----------+
2 | 3 | No.8888 | 7654321
Table3条数据:
tid | subject | desc
----+---------+-------------+
1 | Test 1 | Desc 1
2 | Test 2 | Desc 2
3 | Test 3 | Desc 3
4 | Test 4 | Desc 4
我想这样组合:
tid | subject | type_1 | type_2 | address_1 | address_2 | contact_1 | contact_2 | desc
----+---------+--------+--------+-----------+-----------+-----------+-----------+------
1 | Test 1 | 4 | | No.2123 | |01234567 | | Desc1
2 | Test 2 | | 3 | |No.8888 | |7654321 | Desc2
4 | Test 4 | 4 | | No.4567 | |00011234 | | Desc4
如果 table 1 得到 "tid" 1,那么 table 2 永远不会得到相同的时间。
如果在 table 1 和 table 2 中找不到 tid,则忽略
谢谢!
我之前的代码是:
$query = mysql_query("SELECT table1.*, table2.*, table3.subject, table3.desc, FROM table1 ON table1.tid = table3.tid LEFT JOIN table2 ON table2.tid = table3.tid LEFT JOIN table3 ON table1.tid AND table2.tid = table3.tid");
您所写的JOIN
子句格式不正确。你应该这样使用它:
`table1 JOIN table2 ON` your conditions to join the table.
您可以从 here 获得更多关于 JOIN
的信息。
所以你可以试试这个:
SELECT table3.tid AS tid,
t3.subject AS subject,
t1.type_1 AS type_1,
t2.type_2 AS type_2,
t1.address_1 AS address_1,
t2.address_2 AS address_2,
t1.contact_1 AS contact_1,
t2.contact_2 AS contact_2,
t3.desc AS description
FROM table3 t3
INNER JOIN table1 t1 ON t3.tid = t1.tid
INNER JOIN table2 t2 ON t3.tid = t2.tid
SELECT
table1.tid,
table3.subject,
table1.type_1,
NULL AS type_2,
table1.address_1,
NULL AS address_2,
table1.contact_1,
NULL AS contact_2,
table3.`desc`
FROM table1
INNER JOIN table3 ON table1.tid=table3.tid
UNION ALL
SELECT
table2.tid,
table3.subject,
NULL AS type_1,
table2.type_2,
NULL AS address_1,
table2.address_2,
NULL AS contact_1,
table2.contact_2,
table3.`desc`
FROM table2
INNER JOIN table3 ON table2.tid=table3.tid
DROP TABLE IF EXISTS table1;
CREATE TABLE table1
(tid INT NOT NULL
,type_1 INT NOT NULL
,address_1 VARCHAR(12) NOT NULL
,contact_1 VARCHAR(12) NOT NULL
);
INSERT INTO table1 VALUES
(1 ,4 ,'No.2123','01234567'),
(4 ,4 ,'No.4567','00011234');
DROP TABLE IF EXISTS table2;
CREATE TABLE table2
(tid INT NOT NULL
,type_2 INT NOT NULL
,address_2 VARCHAR(12) NOT NULL
,contact_2 VARCHAR(12) NOT NULL
);
INSERT INTO table2 VALUES
(2 ,3 ,'No.8888','7654321');
DROP TABLE IF EXISTS table3;
CREATE TABLE table3
(tid INT NOT NULL
,subject VARCHAR(12) NOT NULL
,description VARCHAR(12) NOT NULL
);
INSERT INTO table3 VALUES
(1 ,'Test 1','Desc 1'),
(2 ,'Test 2','Desc 2'),
(3 ,'Test 3','Desc 3'),
(4 ,'Test 4','Desc 4');
SELECT x.*
, y.subject
, y.description
FROM
( SELECT tid
, type_1
, NULL type_2
, address_1
, contact_1
, NULL address_2
, NULL contact_2
FROM table1
UNION
SELECT tid
, NULL
, type_2
, NULL
, NULL
, address_2
, contact_2
FROM table2
) x
JOIN table3 y
ON y.tid = x.tid;
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| tid | type_1 | type_2 | address_1 | contact_1 | address_2 | contact_2 | subject | description |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| 1 | 4 | NULL | No.2123 | 01234567 | NULL | NULL | Test 1 | Desc 1 |
| 2 | NULL | 3 | NULL | NULL | No.8888 | 7654321 | Test 2 | Desc 2 |
| 4 | 4 | NULL | No.4567 | 00011234 | NULL | NULL | Test 4 | Desc 4 |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
3 rows in set (0.00 sec)