SQL 帮助内连接左连接
SQL Help Inner JOIN LEFT JOIN
我的查询是:
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID, Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female'
ORDER BY Pics.ID
结果是:
ID ProfileID Position RateID Rating ProfileID Gender
23 24 1 59 9 42 female
24 24 2 33 8 32 female
23 24 1 53 3 40 female
26 24 4 31 8 32 female
30 25 4 30 8 32 female
24 24 2 58 4 42 female
现在我想做另一个查询:
如果 Rate.ProfileID = 32,删除包含相同 Pics.ID
的任何行
剩下:
ID ProfileID Position RateID Rating ProfileID Gender
23 24 1 59 9 42 female
23 24 1 53 3 40 female
并删除任何重复的 Pics.ID 所以只是上面的一个,因为它们都是 = 23 所以剩下 :
23 24 1 59 9 42 女性或 23 24 1 53 3 40 女性
@Shadow Because the 2nd row contains the Rate.ProfileID = 32, and that
Pic.ID = 24, therefore it must remove ALL Pic.ID = 24, which removes
the bottom row also.
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID, Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female' AND Pics.ID NOT IN (
SELECT Pics.ID
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female' AND Rate.ProfileID = 32)
ORDER BY Pics.ID
您可能应该去掉 "magical numbers",比如 32。也就是说,我认为这会满足您的需求。
SELECT
P.ID,
P.ProfileID,
P.Position,
R.ID as RateID,
R.Rating,
R.ProfileID,
PR.Gender
FROM
Pics P
INNER JOIN Profiles PR ON PR.ID = P.ProfileID
LEFT JOIN Rate R ON R.PicID = P.ID
WHERE
PR.Gender = 'female' AND
NOT EXISTS (
SELECT *
FROM Pics P2
INNER JOIN Profiles PR2 ON PR2.ID = P2.ProfileID
INNER JOIN Rate R2 ON R2.PicID = P2.ID AND R2.ProfileID = 32
WHERE
P2.ID = P.ID
)
ORDER BY
P.ID
试试这个:
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID,
Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
LEFT JOIN Rate AS r
ON Rate.ProfileID = r.ProfileID AND Rate.ID > r.ID
WHERE Gender = 'female' AND (Pics.ProfileID <> 32) AND (r.ID IS NULL)
ORDER BY Pics.ID
最后一个 LEFT JOIN
操作有助于识别在 ON
子句中使用此谓词的重复项:
Rate.ProfileID = r.ProfileID
如果存在此类重复项,如 Rate.ProfileID = 42
的情况,则由于以下条件,仅返回具有 最大值 Rate.ID
的副本:
Rate.ID > r.ID (`ON` clause)
和
(r.ID IS NULL) (`WHERE` clause)
我的查询是:
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID, Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female'
ORDER BY Pics.ID
结果是:
ID ProfileID Position RateID Rating ProfileID Gender
23 24 1 59 9 42 female
24 24 2 33 8 32 female
23 24 1 53 3 40 female
26 24 4 31 8 32 female
30 25 4 30 8 32 female
24 24 2 58 4 42 female
现在我想做另一个查询: 如果 Rate.ProfileID = 32,删除包含相同 Pics.ID
的任何行剩下:
ID ProfileID Position RateID Rating ProfileID Gender
23 24 1 59 9 42 female
23 24 1 53 3 40 female
并删除任何重复的 Pics.ID 所以只是上面的一个,因为它们都是 = 23 所以剩下 :
23 24 1 59 9 42 女性或 23 24 1 53 3 40 女性
@Shadow Because the 2nd row contains the Rate.ProfileID = 32, and that Pic.ID = 24, therefore it must remove ALL Pic.ID = 24, which removes the bottom row also.
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID, Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female' AND Pics.ID NOT IN (
SELECT Pics.ID
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
WHERE Gender = 'female' AND Rate.ProfileID = 32)
ORDER BY Pics.ID
您可能应该去掉 "magical numbers",比如 32。也就是说,我认为这会满足您的需求。
SELECT
P.ID,
P.ProfileID,
P.Position,
R.ID as RateID,
R.Rating,
R.ProfileID,
PR.Gender
FROM
Pics P
INNER JOIN Profiles PR ON PR.ID = P.ProfileID
LEFT JOIN Rate R ON R.PicID = P.ID
WHERE
PR.Gender = 'female' AND
NOT EXISTS (
SELECT *
FROM Pics P2
INNER JOIN Profiles PR2 ON PR2.ID = P2.ProfileID
INNER JOIN Rate R2 ON R2.PicID = P2.ID AND R2.ProfileID = 32
WHERE
P2.ID = P.ID
)
ORDER BY
P.ID
试试这个:
SELECT Pics.ID, Pics.ProfileID, Pics.Position, Rate.ID as RateID,
Rate.Rating, Rate.ProfileID, Gender
FROM Pics
INNER JOIN Profiles ON Pics.ProfileID = Profiles.ID
LEFT JOIN Rate ON Pics.ID = Rate.PicID
LEFT JOIN Rate AS r
ON Rate.ProfileID = r.ProfileID AND Rate.ID > r.ID
WHERE Gender = 'female' AND (Pics.ProfileID <> 32) AND (r.ID IS NULL)
ORDER BY Pics.ID
最后一个 LEFT JOIN
操作有助于识别在 ON
子句中使用此谓词的重复项:
Rate.ProfileID = r.ProfileID
如果存在此类重复项,如 Rate.ProfileID = 42
的情况,则由于以下条件,仅返回具有 最大值 Rate.ID
的副本:
Rate.ID > r.ID (`ON` clause)
和
(r.ID IS NULL) (`WHERE` clause)