SQL Join结果在何处?

时间:2014-07-31 13:00:10

标签: mysql sql join where-clause clause

示例是实际代码和实际表。我只是切断了桌子的底部。分组按他们分组,但我想删除那些不符合两个标准但不合并两次标准的人

tag_tags:

    id  category  name             
------  --------  -----------------
     1         1  England          
     2         1  Wales            

tag_usertags:

    id  userid   tagid  
------  ------  --------
    30      10         2
    31      10        11

tag_category

    id  name        enabled  
------  ----------  ---------
     1  Country             1
     2  Region              1

查询:

SELECT u.id AS userid,
    u.firstname AS firstname,
    u.firstname AS lastname,
    u.username AS username,
    tc.name AS category,
    tt.name AS tag
        FROM user u
            JOIN tag_usertags ut ON (ut.userid = u.id)
            JOIN tag_tags tt ON (tt.id = ut.tagid)
            JOIN tag_category tc ON (tc.id = tt.category)
                WHERE tt.id = "13"
                OR tt.id = "31";

结果:

userid  firstname  lastname  username                        category    tag              
------  ---------  --------  ------------------------------  ----------  -----------------
    10  Manager    Manager   manager                         Region      Vastra Gotaland  
    14  Abigail    Abigail   abigail                         Region      Vastra Gotaland  
    10  Manager    Manager   manager                         Department  Support   

期望的结果:

userid  firstname  lastname  username                        category    tag              
------  ---------  --------  ------------------------------  ----------  -----------------
    10  Manager    Manager   manager                         Region      Vastra Gotaland

如果两个部分都为真,则仅显示与tags

匹配的用户

0 个答案:

没有答案