问题
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