0

我正在尝试通过将表 Customer 与已加入的 BuyVehicle & Vehicle 表连接起来来获得成对的匹配列,该表我已给出别名“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;

错误 1054 (42S22):“on 子句”中的未知列“p1.SSN”

4

2 回答 2

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
于 2012-10-08T01:12:31.273 回答
0
  1. 什么是 & - 没有这样的 SQL.. 更改为 AND
  2. 这是不正确的 SELECT BVSSN, BVVin, Vin FROM CarPurchase INNER JOIN Car ON BVVin = Vin

    SELECT CarPurchase.BVSSN, Car.BVVin, Car.Vin FROM CarPurchase INNER JOIN Car ON CarPurchase.BVVin = Car.Vin

否则使用别名

于 2012-10-07T23:45:10.350 回答