MYSQL QUERY LEFT JOIN SHOW ALL DATA FROM ONE TABLE

狂风中的少年 提交于 2020-01-04 04:38:06

问题


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.

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?


回答1:


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:


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:


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



来源:https://stackoverflow.com/questions/17787084/mysql-query-left-join-show-all-data-from-one-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!