Mysql 如何在这个多连接语句中使用GROUP BY语法和CASE语句?

Mysql 如何在这个多连接语句中使用GROUP BY语法和CASE语句?,mysql,sql,Mysql,Sql,我有以下疑问 SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id FROM s s INNER JOIN c c ON s.c_id=c.c_id INNER JOIN sm sm ON s.t_id = sm.t_id WHERE s.c_id=8; 返回以下结果集 s.s_id s.t_id c.c_id c.desc sm.user_id 3 123 8 something 2 3

我有以下疑问

SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id
FROM s s
INNER JOIN c c
ON s.c_id=c.c_id
INNER JOIN sm sm
ON s.t_id = sm.t_id
WHERE s.c_id=8;
返回以下结果集

s.s_id  s.t_id  c.c_id  c.desc     sm.user_id
3       123     8       something  2
3       123     8       something  2
3       123     8       something  1
4       456     8       something  2
4       456     8       something  2
s.s_id  s.t_id  c.c_id  c.desc     sm.user_id
3       123     8       something  2
3       123     8       something  2
3       123     8       something  1
4       456     8       something  2
4       456     8       something  2
我想

  • 在结果集中创建一个附加列,指示 用户拥有该产品 (这涉及使用
    大小写
    语法)
  • 并且只显示那些 唯一的
    s.s\u id
    (这涉及使用
    按s.s\u id分组
  • 例如,如果
    s.c_id=8
    sm.user_id=1
    ,结果集将是

    s.s_id  s.t_id  c.c_id  c.desc      sm.user_id does_user_own_product
    3       123     8       something   1          yes
    4       456     8       something   2          no
    
    s.s_id  s.t_id  c.c_id  c.desc      sm.user_id does_user_own_product
    3       123     8       something   1          yes
    4       456     8       something   2          yes
    
    s.s\u id=3
    时,
    dose\u user\u own\u product
    的值为
    yes
    ,因为至少有一个
    sm.user\u id=1,其中s.s\u id=3
    。 当
    s.s\u id=4
    时,由于没有
    sm.user\u id=1,其中s.s\u id=4
    ,因此
    does\u user\u own\u product
    的值为
    no

    例如,如果
    s.c_id=8
    sm.user_id=2
    ,结果集将是

    s.s_id  s.t_id  c.c_id  c.desc      sm.user_id does_user_own_product
    3       123     8       something   1          yes
    4       456     8       something   2          no
    
    s.s_id  s.t_id  c.c_id  c.desc      sm.user_id does_user_own_product
    3       123     8       something   1          yes
    4       456     8       something   2          yes
    
    s.s\u id=3
    时,
    dose\u user\u own\u product
    的值为
    yes
    ,因为至少有一个
    sm.user\u id=2,其中s.s\u id=3
    。 当
    s.s\u id=4
    时,
    dose\u user\u own\u product
    的值为
    yes
    ,因为至少有一个
    sm.user\u id=2,其中s.s\u id=4

    如果我提供
    s.c_id
    sm.user_id

    编辑 我意识到,对于用户拥有产品意味着什么,存在一些困惑

    如果可以在sm.user\u id中找到用户的id,则该用户拥有该s.s\u id

    例如,在原始结果集中

    s.s_id  s.t_id  c.c_id  c.desc     sm.user_id
    3       123     8       something  2
    3       123     8       something  2
    3       123     8       something  1
    4       456     8       something  2
    4       456     8       something  2
    
    s.s_id  s.t_id  c.c_id  c.desc     sm.user_id
    3       123     8       something  2
    3       123     8       something  2
    3       123     8       something  1
    4       456     8       something  2
    4       456     8       something  2
    
    用户1和2拥有s.s_id 3,只有用户2拥有s.s_id 4

    使用MySql的优势:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
        bit_or(user_id = @userInquired) as does_user_own_product
    from tbl
    group by s_id;
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
        bit_or(user_id = @userInquired) as does_user_own_product
    from tbl
    group by s_id;
    
    公共分母SQL:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
    
      case when sum(case when user_id = @userInquired then 1 end) > 0 then
         1
      else
         0
      end as does_user_own_product
    
    from tbl
    group by s_id;
    
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
    
      case when sum(case when user_id = @userInquired then 1 end) > 0 then
         1
      else
         0
      end as does_user_own_product
    
    from tbl
    group by s_id;
    

    公共分母SQL。最短技术如果您的数据库没有正确的布尔值,请在和max时使用
    大小写组合:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
    
      max(case when user_id = @userInquired then 1 else 0 end) 
           as does_user_own_product
    
    from tbl
    group by s_id;
    
    
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
    
      max(case when user_id = @userInquired then 1 else 0 end) 
           as does_user_own_product
    
    from tbl
    group by s_id;
    
    这样做:

    使用MySql的优势:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
        bit_or(user_id = @userInquired) as does_user_own_product
    from tbl
    group by s_id;
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
        bit_or(user_id = @userInquired) as does_user_own_product
    from tbl
    group by s_id;
    
    公共分母SQL:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
    
      case when sum(case when user_id = @userInquired then 1 end) > 0 then
         1
      else
         0
      end as does_user_own_product
    
    from tbl
    group by s_id;
    
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
    
      case when sum(case when user_id = @userInquired then 1 end) > 0 then
         1
      else
         0
      end as does_user_own_product
    
    from tbl
    group by s_id;
    

    公共分母SQL。最短技术如果您的数据库没有正确的布尔值,请在
    和max时使用
    大小写组合:

    set @userInquired := 1;
    
    select s_id, t_id, c_id, dsc, 
    
      max(case when user_id = @userInquired then 1 else 0 end) 
           as does_user_own_product
    
    from tbl
    group by s_id;
    
    
    
    set @userInquired := 2;
    
    select s_id, t_id, c_id, dsc, 
    
      max(case when user_id = @userInquired then 1 else 0 end) 
           as does_user_own_product
    
    from tbl
    group by s_id;
    
    也许是这样:

    SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id,
      MAX(sm.user_id = @userid) AS does_user_own_product
    FROM s s
    INNER JOIN c c
    ON s.c_id=c.c_id
    INNER JOIN sm sm
    ON s.t_id = sm.t_id
    WHERE s.c_id=8
    GROUP BY s.s_id;
    
    选择s.s\u id、s.t\u id、c.c\u id、c.desc、sm.user\u id、,
    MAX(sm.user\u id=@userid)和用户拥有的产品一样
    从s
    内连接
    在s.c_id=c.c_id上
    内连接sm
    在s.t_id=sm.t_id上
    其中s.c_id=8
    按s.s_id分组;
    
    尽管,老实说,我认为拉入既不包含在GROUP BY中也不聚合的列(如
    c.c_id
    c.desc
    sm.user_id
    )没有多大意义。(是的,MySQL确实允许您这样做,但这些值在您的情况下似乎没有多大意义。)

    可能是这样的:

    SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id,
      MAX(sm.user_id = @userid) AS does_user_own_product
    FROM s s
    INNER JOIN c c
    ON s.c_id=c.c_id
    INNER JOIN sm sm
    ON s.t_id = sm.t_id
    WHERE s.c_id=8
    GROUP BY s.s_id;
    
    选择s.s\u id、s.t\u id、c.c\u id、c.desc、sm.user\u id、,
    MAX(sm.user\u id=@userid)和用户拥有的产品一样
    从s
    内连接
    在s.c_id=c.c_id上
    内连接sm
    在s.t_id=sm.t_id上
    其中s.c_id=8
    按s.s_id分组;
    

    尽管,老实说,我认为拉入既不包含在GROUP BY中也不聚合的列(如
    c.c_id
    c.desc
    sm.user_id
    )没有多大意义。(是的,MySQL确实允许您这样做,但这些值在您的情况下似乎没有多大意义。)

    不清楚。哪列是产品?如果至少一条
    sm.user\u id
    记录的值与每个
    s.s\u id
    sm.user\u id
    的指定值匹配,则该用户“拥有”产品。这有意义吗?这有点令人困惑。我刚刚编辑了我的原始帖子,澄清了“拥有”一个产品意味着什么,这还不清楚。哪列是产品?如果至少一条
    sm.user\u id
    记录的值与每个
    s.s\u id
    sm.user\u id
    的指定值匹配,则该用户“拥有”产品。这有意义吗,有点让人困惑刚刚编辑了我的原始帖子,澄清了“拥有”一个产品意味着什么谢谢Michael,我现在就测试一下,几分钟后再给你回复在SqlFiddle上检查一下,快速测试一下ツ它看起来很棒,小提琴也能工作,我只需要更改变量名,使其在我的系统上工作。您可以通过使用
    max
    而不是
    sum
    并将
    else 0
    添加到“internal”中来消除“external”
    case
    case
    。用于其他数据库:-)例如,使用Sql Server时无法删除case。如果一个被询问的用户在组中出现两次或两次以上,那么some将是两次或两次以上,因此仍然需要外部
    case when>0
    谢谢Michael,他将立即测试这个问题,并在几分钟后返回给您在SqlFiddle上检查它以快速测试它ツ它看起来很棒,小提琴也能工作,我只需要更改变量名,使其在我的系统上工作。您可以通过使用
    max
    而不是
    sum
    并将
    else 0
    添加到“internal”中来消除“external”
    case
    case
    。用于其他数据库:-)例如,使用Sql Server时无法删除case。如果一个被询问的用户在组中出现两次或两次以上,则部分用户将出现两次或两次以上,因此当>0时仍然需要外部
    案例