sql选择使用相同的表进行连接

时间:2014-01-08 15:49:27

标签: mysql sql select join

使用mysql 4/5

我有两张桌子:

ROUTE
route_order (int)
zipcode

LOADS
load_id
pu_zipcode
do_zipcode

目标:

从Loads中选择所有行 pu_zipcode和do_zipcode在Route AND中 其中route_order pu_zipcode< route_order do_zipcode

2 个答案:

答案 0 :(得分:1)

我认为这就是你要问的问题:

 SELECT L.*
 FROM Loads AS L
 JOIN Route AS R1
   ON R1.zipcode = L.PU_zipcode
 JOIN Route AS R2
   ON R2.zipcode = L.do_zipcode
 WHERE R1.ROUTE_ORDER < R2.ROUTE_ORDER

答案 1 :(得分:0)

尝试此查询

SELECT l.load_id, l.pu_zipcode, l.do_zipcode 
FROM LOADS AS l, ROUTE AS r1, ROUTE AS r2
WHERE l.pu_zipcode=r1.zipcode
AND l.do_zipcode=r2.zipcode
AND r1.route_order<r2.route_order