2012-05-14 44 views
8

Tôi đang cố gắng thực hiện nhiều lệnh LEFT JOIN trên cùng một cột của một bảng. Tôi cần LEFT JOIN "table2.words" với "table1.color" và "table2.words" với "table1.food". Làm thế nào để tôi làm điều này? và tôi có thể làm điều đó bằng cách làm cho bên trái tham gia "table2.words" một cột mới không?LEFT JOIN AS cột mới?

Mã của tôi SQL:

SELECT table1.id, table1.color, table2.words 
FROM table1 
LEFT JOIN table2 ON table1.color=table2.id 
LEFT JOIN table2 ON table1.food=table2.id 

table1:

-------------------------------- 
| id  | color | food  | 
-------------------------------- 
| 1  | 1  | 3  | 
| 2  | 1  | 4  | 
| 3  | 1  | 3  | 
| 4  | 1  | 4  | 
| 5  | 2  | 3  | 
| 6  | 2  | 4  | 
| 7  | 2  | 3  | 
| 8  | 2  | 4  | 
-------------------------------- 

table2:

--------------------------- 
| id  | words   | 
--------------------------- 
| 1  | yellow   | 
| 2  | blue   | 
| 3  | cookies  | 
| 4  | milk   | 
--------------------------- 

Điều tôi cố gắng để xuất:

---------------------------------------- 
| id  | colorname | foodname  | 
---------------------------------------- 
| 1  | yellow  | cookies  | 
| 2  | yellow  | milk   | 
| 3  | yellow  | cookies  | 
| 4  | yellow  | milk   | 
| 5  | blue   | cookies  | 
| 6  | blue   | milk   | 
| 7  | blue   | cookies  | 
| 8  | blue   | milk   | 
---------------------------------------- 

Lưu ý: Tôi không thể thay đổi cấu trúc bảng.

+0

Từ dữ liệu bạn đang trình bày, có vẻ như điều này có thể được thực hiện với các kết nối thẳng, bên trong - các kết nối bên trái thực sự cần thiết phải không? –

Trả lời

21
SELECT 
    table1.id, 
    table2_A.words colorname, 
    table2_B.words foodname 
FROM table1 
LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
LEFT JOIN table2 table2_B ON table1.food=table2_B.id; 

dữ liệu mẫu của bạn

mysql> drop database if exists supercoolville; 
Query OK, 2 rows affected (0.06 sec) 

mysql> create database supercoolville; 
Query OK, 1 row affected (0.00 sec) 

mysql> use supercoolville; 
Database changed 
mysql> create table table1 
    -> (
    ->  id int not null auto_increment, 
    ->  color int, 
    ->  food int, 
    ->  primary key (id) 
    ->); 
Query OK, 0 rows affected (0.06 sec) 

mysql> insert into table1 (color,food) values 
    -> (1,3),(1,4),(1,3),(1,4), 
    -> (2,3),(2,4),(2,3),(2,4); 
Query OK, 8 rows affected (0.06 sec) 
Records: 8 Duplicates: 0 Warnings: 0 

mysql> create table table2 
    -> (
    ->  id int not null auto_increment, 
    ->  words varchar(20), 
    ->  primary key (id) 
    ->); 
Query OK, 0 rows affected (0.05 sec) 

mysql> insert into table2 (words) values 
    -> ('yellow'),('blue'),('cookies'),('milk'); 
Query OK, 4 rows affected (0.07 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> select * from table1; 
+----+-------+------+ 
| id | color | food | 
+----+-------+------+ 
| 1 |  1 | 3 | 
| 2 |  1 | 4 | 
| 3 |  1 | 3 | 
| 4 |  1 | 4 | 
| 5 |  2 | 3 | 
| 6 |  2 | 4 | 
| 7 |  2 | 3 | 
| 8 |  2 | 4 | 
+----+-------+------+ 
8 rows in set (0.01 sec) 

mysql> select * from table2; 
+----+---------+ 
| id | words | 
+----+---------+ 
| 1 | yellow | 
| 2 | blue | 
| 3 | cookies | 
| 4 | milk | 
+----+---------+ 
4 rows in set (0.00 sec) 

Kết quả truy vấn của tôi

mysql> SELECT 
    ->  table1.id, 
    ->  table2_A.words colorname, 
    ->  table2_B.words foodname 
    -> FROM table1 
    ->  LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
    ->  LEFT JOIN table2 table2_B ON table1.food=table2_B.id 
    -> ; 
+----+-----------+----------+ 
| id | colorname | foodname | 
+----+-----------+----------+ 
| 1 | yellow | cookies | 
| 2 | yellow | milk  | 
| 3 | yellow | cookies | 
| 4 | yellow | milk  | 
| 5 | blue  | cookies | 
| 6 | blue  | milk  | 
| 7 | blue  | cookies | 
| 8 | blue  | milk  | 
+----+-----------+----------+ 
8 rows in set (0.00 sec) 

mysql> 

CẬP NHẬT 2012-05-14 19:10 EDT

Trong trường hợp có giá trị cho thực phẩm hoặc màu sắc không tồn tại, đây là truy vấn được điều chỉnh:

SELECT 
    table1.id, 
    IFNULL(table2_A.words,'Unknown Color') colorname, 
    IFNULL(table2_B.words,'Unknown Food') foodname 
FROM table1 
LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
LEFT JOIN table2 table2_B ON table1.food=table2_B.id; 

tôi sẽ thêm hàng để table1 và chạy truy vấn mới này

mysql> drop database if exists supercoolville; 
Query OK, 2 rows affected (0.13 sec) 

mysql> create database supercoolville; 
Query OK, 1 row affected (0.00 sec) 

mysql> use supercoolville; 
Database changed 
mysql> create table table1 
    -> (
    ->  id int not null auto_increment, 
    ->  color int, 
    ->  food int, 
    ->  primary key (id) 
    ->); 
Query OK, 0 rows affected (0.08 sec) 

mysql> insert into table1 (color,food) values 
    -> (1,3),(1,4),(1,3),(1,4), 
    -> (2,3),(2,4),(2,3),(2,4), 
    -> (5,3),(5,4),(2,6),(2,8); 
Query OK, 12 rows affected (0.07 sec) 
Records: 12 Duplicates: 0 Warnings: 0 

mysql> create table table2 
    -> (
    ->  id int not null auto_increment, 
    ->  words varchar(20), 
    ->  primary key (id) 
    ->); 
Query OK, 0 rows affected (0.08 sec) 

mysql> insert into table2 (words) values 
    -> ('yellow'),('blue'),('cookies'),('milk'); 
Query OK, 4 rows affected (0.06 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> select * from table1; 
+----+-------+------+ 
| id | color | food | 
+----+-------+------+ 
| 1 |  1 | 3 | 
| 2 |  1 | 4 | 
| 3 |  1 | 3 | 
| 4 |  1 | 4 | 
| 5 |  2 | 3 | 
| 6 |  2 | 4 | 
| 7 |  2 | 3 | 
| 8 |  2 | 4 | 
| 9 |  5 | 3 | 
| 10 |  5 | 4 | 
| 11 |  2 | 6 | 
| 12 |  2 | 8 | 
+----+-------+------+ 
12 rows in set (0.00 sec) 

mysql> select * from table2; 
+----+---------+ 
| id | words | 
+----+---------+ 
| 1 | yellow | 
| 2 | blue | 
| 3 | cookies | 
| 4 | milk | 
+----+---------+ 
4 rows in set (0.00 sec) 

mysql> SELECT 
    ->  table1.id, 
    ->  IFNULL(table2_A.words,'Unknown Color') colorname, 
    ->  IFNULL(table2_B.words,'Unknown Food') foodname 
    -> FROM table1 
    -> LEFT JOIN table2 table2_A ON table1.color=table2_A.id 
    -> LEFT JOIN table2 table2_B ON table1.food=table2_B.id; 
+----+---------------+--------------+ 
| id | colorname  | foodname  | 
+----+---------------+--------------+ 
| 1 | yellow  | cookies  | 
| 2 | yellow  | milk   | 
| 3 | yellow  | cookies  | 
| 4 | yellow  | milk   | 
| 5 | blue   | cookies  | 
| 6 | blue   | milk   | 
| 7 | blue   | cookies  | 
| 8 | blue   | milk   | 
| 9 | Unknown Color | cookies  | 
| 10 | Unknown Color | milk   | 
| 11 | blue   | Unknown Food | 
| 12 | blue   | Unknown Food | 
+----+---------------+--------------+ 
12 rows in set (0.00 sec) 

mysql> 

Với bất kỳ dữ liệu không hợp lệ, LEFT JOIN trong vẫn cần thiết.

+1

Tôi cúi đầu trước sự khiếp sợ của bạn. –

+0

THANK YOU VERY MUCH !!!!!!!!!!!!!!!!!!!! – supercoolville

3

thử:

SELECT table1.id, colorcodes.words, foodcodes.words 
FROM table1 
LEFT JOIN table2 as colorcodes 
    ON colorcodes.id = table1.color 
LEFT JOIN table2 as foodcodes 
    ON foodcodes.id= table1.food 
3

Đây là truy vấn:

SELECT a.id as id, b.words as colorname, c.words as foodname 
FROM table1 a 
    LEFT JOIN table2 b ON b.id = a.color 
    LEFT JOIN table2 c ON c.id = a.food 

Lưu ý: Có vẻ từ dữ liệu của bạn mà một LEFT JOIN là không cần thiết. Nếu không có hàng nào trong bảng 1, trong đó màu hoặc thực phẩm là rỗng, thì bạn có thể rời khỏi số LEFT.

+0

Điều này cũng làm việc! Cảm ơn bạn!!! – supercoolville