MYSQL查询左连接显示来自一个表的所有数据

时间:2022-07-26 03:35:13
SELECT 
A.CODE,
B.NOTE,
C.NUMBER

FROM (A
LEFT JOIN B
ON A.CODE = B.CODE
LEFT JOIN C
ON A.CODE = C.NUMBER
)

WHERE C.ID = B.ID 

Need to show some results combined from 3 tables, but my results show all data from table B reported also if the data isn't real from table C.

需要显示结合了3个表的一些结果,但是如果数据不是来自表C的,我的结果也显示了表B报告的所有数据。

Table A.code
1
2
3

Table B.code
1
2
3

Table B.note
pippo 
paperino
pluto

Table C.number
1

Ideally there should be one result showing

理想情况下应该有一个结果显示

1  1    pippo

but in results it is shown:

但结果显示:

1  1    pippo
1  1    paperino
1  1    pluto

How can I get the real data?

我怎样才能得到真实的数据?

3 个解决方案

#1


3  

SELECT 
A.CODE,
B.NOTE,
C.NUMBER

FROM A
INNER JOIN B ON A.CODE = B.CODE
INNER JOIN C ON A.CODE = C.NUMBER

no need for WHERE cluse

不需要在哪里开玩笑

#2


1  

You need INNER JOIN

你需要内连接

SELECT 
A.CODE,
B.NOTE,
C.NUMBER

FROM C
INNER JOIN B on C.Number=B.Code
INNER JOIN A on B.Code=A.Code

#3


0  

LEFT JOIN will take all results from the table mentioned on the left side. Try JOIN instead which is an INNER JOIN afaik

左连接将从左侧的表中获取所有结果。试试JOIN,它是一个内部连接

#1


3  

SELECT 
A.CODE,
B.NOTE,
C.NUMBER

FROM A
INNER JOIN B ON A.CODE = B.CODE
INNER JOIN C ON A.CODE = C.NUMBER

no need for WHERE cluse

不需要在哪里开玩笑

#2


1  

You need INNER JOIN

你需要内连接

SELECT 
A.CODE,
B.NOTE,
C.NUMBER

FROM C
INNER JOIN B on C.Number=B.Code
INNER JOIN A on B.Code=A.Code

#3


0  

LEFT JOIN will take all results from the table mentioned on the left side. Try JOIN instead which is an INNER JOIN afaik

左连接将从左侧的表中获取所有结果。试试JOIN,它是一个内部连接