SQL一个id JOIN两个名字

时间:2014-03-27 21:27:27

标签: mysql sql join

我有2张桌子。我想询问color1和color2的名字。

id  color
0   white
1   black
2   blue
--------------------------     
carid   color1  color2
560     1   0
480     2   1

我试过这个,但它确实效果不好。虽然color2有color1-s名称。

SELECT v.csapatnev,v.nev,v.navigator,vt.versenynev,a.autonev,g.geposztalynev,c.color
FROM verseny v 
JOIN versenytipus vt  ON v.versenyid=vt.id
JOIN autok a ON v.autoid=a.autoid
JOIN geposztalyok g ON a.geposztalyid=g.id
JOIN colors c ON c.id=v.color1
ORDER BY v.csapatnev

1 个答案:

答案 0 :(得分:1)

这样做。

SELECT c1.color as color1, c2.color as color2
FROM CAR car
INNER JOIN COLOR c1 ON c1.id = car.color1
INNER JOIN COLOR c2 ON c2.id = car.color2