这会是什么样的SQL连接?

时间:2010-01-15 20:44:56

标签: sql expressionengine

我需要去两张桌子才能获得相应的信息

exp_member_groups
-group_id
-group_title

exp_members
-member_id
-group_id

我有适当的member_id

所以我需要检查成员表,获取group_id,然后转到groups表并匹配group_id并从中获取group_title。

3 个答案:

答案 0 :(得分:4)

INNER JOIN:

SELECT exp_member_groups.group_title
FROM exp_members
INNER JOIN exp_member_groups ON exp_members.group_id = exp_member_groups.group_id
WHERE exp_members.member_id = @memberId

答案 1 :(得分:2)

SELECT g.group_title
FROM exp_members m
    JOIN exp_member_groups g ON m.group_id = g.group_id
WHERE m.member_id = @YourMemberId

答案 2 :(得分:1)

如果总是有匹配的组,或者您只想要行,那么它将是INNER JOIN

SELECT  g.group_title
FROM    exp_members m
        INNER JOIN
                exp_member_groups g
                ON m.group_id = g.group_id
WHERE   m.member_id = @member_id

如果您想要行甚至group_id不匹配的行,那么它就是LEFT JOIN - 在上面用INNER JOIN替换LEFT JOIN