0

我有以下查询

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

例如,如果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_productyes因为至少有一个sm.user_id=1 WHERE s.s_id=3。当s.s_id=4, 的值does_user_own_productno因为没有 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_productyes因为至少有一个sm.user_id=2 WHERE s.s_id=3。当s.s_id=4, 的值does_user_own_productyes因为至少有一个 sm.user_id=2 WHERE s.s_id=4

给定我提供的值s.c_idsm.user_id

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

如果用户的 id 可以在 sm.user_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

4

2 回答 2

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 whenand的组合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;
于 2012-05-10T01:04:53.910 回答
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.desc、 )并没有多大意义。sm.user_id(是的,MySQL 确实允许您这样做,但这些值在您的情况下似乎没有多大意义。)

于 2012-05-10T01:12:50.700 回答