2

这是查询:

SELECT *
FROM square_achievements_achievements
JOIN square_achievements_achievement_counters ON square_achievements_achievement_counters.SAA_ID = square_achievements_achievements.SAA_ID
JOIN square_achievements_counters ON square_achievements_counters.SAC_ID = square_achievements_achievement_counters.SAC_ID
WHERE square_achievements_counters.eventObject = 'CommentEvent'
AND square_achievements_counters.eventType = 'add'
AND square_achievements_achievements.SAA_ID NOT IN
(
    SELECT square_achievements_achievements.SAA_ID 
    FROM square_achievements_achievements 
    JOIN square_achievements_user_achievements ON square_achievements_user_achievements.SAA_ID = square_achievements_achievements.SAA_ID
    WHERE square_achievements_user_achievements.UID = 83
)

如果可能的话,将此查询编写为连接会更有效吗?如果是这样,它将如何重写?

4

2 回答 2

1
SELECT  a.*
FROM    achievements a
JOIN    achievement_counters ac
ON      ac.saac_id = a.saa_id
JOIN    counters c
ON      c.sac_id = ac.sac_id
WHERE   c.eventObject = 'CommentEvent'
        AND c.eventType = 'add'
        AND a.saa_id NOT IN
        (
        SELECT  saa_id
        FROM    user_achievements 
        WHERE   uid = 83
        )

这很不错。

如果你想加入,使用这个:

SELECT  a.*
FROM    achievements a
JOIN    achievement_counters ac
ON      ac.saac_id = a.saa_id
JOIN    counters c
ON      c.sac_id = ac.sac_id
LEFT JOIN
        user_achievements ua
ON      ua.uid = 83
        AND ua.saa_id = a.saa_id
WHERE   c.eventObject = 'CommentEvent'
        AND c.eventType = 'add'
        AND ua.saa_id IS NULL
于 2012-06-01T12:37:07.887 回答
0

我更喜欢使用相关名称来提高代码的可读性

尝试这个:

SELECT * 
FROM square_achievements_achievements a
JOIN square_achievements_achievement_counters b  ON b.SAAC_ID = a.SAA_ID 
JOIN square_achievements_counters c ON c.SAC_ID = b.SAC_ID 
JOIN square_achievements_user_achievements d on d.SAA_ID = a.SAA_ID 
JOIN square_achievements_user_achievements u on d.SAA_ID = u.SAA_ID AND u.UID = 83 
WHERE c.eventObject = 'CommentEvent' 
AND a.eventType = 'add' 
于 2012-06-01T12:37:51.850 回答