多个连接的on子句中的未知列

时间:2012-10-07 23:30:20

标签: mysql sql join

我正在尝试通过将表Customer与已经加入的BuyVehicle&表结合起来获得一对匹配列。我给别名't'的车辆。如果我在第一行分配了别名,那么p1.SSN和我如何假设p2.SSN将跟随一个未知列?

SELECT p1.SSN, p2.SSN FROM Customer AS p1, Customer AS p2 
INNER JOIN (SELECT BVSSN, BVVin, Vin FROM CarPurchase INNER JOIN Car ON BVVin = Vin) 
AS t ON p1.SSN = BVSSN & p2.SSN = BVSSN
WHERE p1.SSN < p2.SSN AND //matching columns comparisons yet to be added;
  

ERROR 1054(42S22):'on clause'中未知列'p1.SSN'

2 个答案:

答案 0 :(得分:2)

问题在于混合不同类型的连接。 MySQL无法识别p1表,因为它不是“连接”的一部分。以下内容应解决此问题:

SELECT p1.SSN, p2.SSN
FROM Customer AS p1 cross join
     Customer AS p2 INNER JOIN
     (SELECT BVSSN, BVVin, Vin
      FROM CarPurchase INNER JOIN
           Car
           ON BVVin = Vin
    ) AS t
    ON p1.SSN = BVSSN and p2.SSN = BVSSN
WHERE p1.SSN < p2.SSN AND //matching columns comparisons yet to be added;

实际上,您应该将WHERE子句移到ON子句中:

SELECT p1.SSN, p2.SSN
FROM Customer AS p1 join
     Customer AS p2 
     on  p1.SSN < p2.SS INNER JOIN
     (SELECT BVSSN, BVVin, Vin
      FROM CarPurchase INNER JOIN
           Car
           ON BVVin = Vin
    ) AS t
    ON p1.SSN = BVSSN and p2.SSN = BVSSN

答案 1 :(得分:0)

  1. 什么是&amp; - 没有这样的SQL ..改为AND
  2. 这是不正确的 选择BVSSN,BVVin,Vin来自CarPurchase INNER JOIN Car ON BVVin = Vin

    选择CarPurchase.BVSSN,Car.BVVin,Car.Vin来自CarPurchase INNER JOIN Car ON CarPurchase.BVVin = Car.Vin

  3. 否则使用alises