如何在这个带有CASE语句的multi-JOIN语句中使用GROUP BY语法?

时间:2012-05-10 00:29:00

标签: 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       123     8       something  2
3       123     8       something  1
4       456     8       something  2
4       456     8       something  2

我想

  1. 在结果集中创建一个指示是否的附加列 用户拥有该产品 (这涉及使用CASE语法)
  2. 并且仅显示那些     唯一s.s_id(这涉及使用GROUP BY s.s_id
  3. 例如,如果s.c_id=8sm.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=3时,does_user_own_product的值为yes,因为至少有一个sm.user_id=1 WHERE s.s_id=3。 在s.s_id=4时,does_user_own_product的值为no,因为没有sm.user_id=1 WHERE s.s_id=4

    例如,如果s.c_id=8sm.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          yes
    

    s.s_id=3时,does_user_own_product的值为yes,因为至少有一个sm.user_id=2 WHERE s.s_id=3。 在s.s_id=4时,does_user_own_product的值为yes,因为至少有一个sm.user_id=2 WHERE s.s_id=4

    实现上述两个子集的适当查询是什么,因为我提供的值为s.c_idsm.user_id

    修改 我意识到对用户拥有产品意味着什么感到困惑。

    如果可以在sm.user_id中找到用户的id,则用户拥有该s.s_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
    

    用户1和2拥有s.s_id 3且只有用户2拥有s.s_id 4

2 个答案:

答案 0 :(得分:4)

执行此操作:http://www.sqlfiddle.com/#!2/e4c84/21

使用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。如果您的数据库没有适当的布尔值,请使用最短的技术,使用case whenmax的组合:

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;

答案 1 :(得分:2)

也许是这样的:

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;

虽然,老实说,我没有看到提取既未包含在GROUP BY中也未汇总的列(如c.c_idc.descsm.user_id)。 (是的,MySQL确实允许你这样做,但在你的情况下,这些值似乎没有多大意义。)