如果只找到一条记录,则返回Yes

时间:2013-02-25 20:02:54

标签: mysql sql

我有一个具有这种结构的表:

table
id      | site_id
-------------------
240     | 1
240     | 2
240     | 3
320     | 1
320     | 2
421     | 1
520     | 2
-------------------

300k记录

现在我正在尝试编写一个查询,为每条记录(id)返回yes或no。

例如,如果ID为240 的记录具有site_id 1,则返回“是”,如果它具有2,3等等,则返回“否”

我不知道如何处理它,但这是一个结果样本:

result_table
.-----------------------.
| id        | result    |
|-----------------------|
| 240       |   No      |  -- has a site_id 1, 2 and 3
| 320       |   No      |  -- has a site_id 1 and 2
| 421       |   Yes     |  -- has a site_id 1 only
| 520       |   No      |  -- has a site_id  2
'-----------------------'

这是我到目前为止的查询,但似乎不正确

SELECT CASE WHEN count(id) > 1 THEN 'N' ELSE 'Y' END as Result 
FROM table sm 
WHERE sm.site_id IN (1) 
AND sm.site_id NOT IN (2,3,4,5,6,7,8,9) 
AND id = 240  

更新

SO这是我的完整查询,我添加了@gordon的答案

SELECT 
    m.merchant_name,
    m.merchant_id,
    ut.realname,
    a.affiliate_name,
    (select (case when count(site_id) = 1 then 'Yes' else 'No' end) as result
    from site_merchant sm
    WHERE sm.merchant_id = m.merchant_id
    group by merchant_id) as isTjoos,   -- y or no
    (select (case when count(site_id) = 2 then 'Yes' else 'No' end) as result
    from site_merchant sm
    WHERE sm.merchant_id = m.merchant_id
    group by merchant_id) as isUCOnly
--  isdlkonly   -- y or no
FROM merchant m
LEFT JOIN merchant_editor_assignment mea ON mea.merchant_id = m.merchant_id
LEFT JOIN user_table ut ON ut.user_id = mea.user_id
LEFT JOIN affiliate a ON a.affiliate_id = m.affiliate_id_default

6 个答案:

答案 0 :(得分:3)

我解释了这个问题,因为你想要只有一个site_id值的id。我将问题中的示例作为示例,使用site_id = 1.要执行此操作:

您想使用count(distinct)

select id, (case when count(distinct site_id) = 1 then 'Yes' else 'No' end) as result
from site_merchant sm
group by id

稍微高效的版本是使用min()max(),假设site_id永远不为NULL:

select id, (case when min(site_id) = max(site_id) then 'Yes' else 'No' end) as result
from site_merchant sm
group by id

这是因为min和max通常比count(distinct)处理的处理要少一些。

如果要检查site_id是否为“1”而不是其他任何内容,请将条件and min(site_id) = 1添加到when子句中。

如果你想检查site_id是否为1并且只有一行,那么你可以这样做:

select id, (case when count(site_id) = 1 and min(site_id) = 1 then 'Yes' else 'No' end) as result
from site_merchant sm
group by id

而且,如果你想检查一行只有一行:

select id, (case when count(site_id) = 1 then 'Yes' else 'No' end) as result
from site_merchant sm
group by id

答案 1 :(得分:1)

SELECT
  it,
  CASE WHEN COUNT(CASE WHEN site_id THEN 1 END)=1
            AND COUNT(CASE WHEN site_id!=1 THEN 1 END)=0 THEN 'Yes'
       ELSE 'No'
  END
FROM sm
GROUP BY it

请参阅小提琴here

答案 2 :(得分:0)

您的查询似乎过于复杂。刚开始,为什么IN(1)NOT IN(2,3...9)?当你的“结果样本”显然不希望这样时,为什么限制为单个ID(AND id = 240)?它没有任何意义。怎么样?

SELECT CASE WHEN count(merchant_id) > 1 THEN 'N' ELSE 'Y' END as isTjoos 
FROM site_merchant
GROUP BY site_id;

答案 3 :(得分:0)

我会使用有计数声明。这样的事情:

SELECT site_id
FROM site_merchant
HAVING (count(merchant_id) > 1)
GROUP BY site_id;

答案 4 :(得分:0)

SELECT ID, CASE WHEN EXTRA > 1 THEN 'No' ELSE 'Yes' END AS Result
FROM
(SELECT ID, Sum(site_id) AS Extra
 from myTable
 GROUP BY ID
) AS Test
编辑:我想这应该适用于MySQL。虽然我还没有努力 我们的想法是SUM提升site_id。对于 site_id = 1的记录,总和将为1.

答案 5 :(得分:0)

这是我找到的解决方案。我使用了@Gordons查询来开始,缺少的是site_id,并且不需要group by:

SELECT 
    m.merchant_name,
    m.merchant_id,
    ut.realname,
    a.affiliate_name,
    (select (case when count(*)>0  then 'No' else 'Yes' end) as result
    from site_merchant sm
    WHERE sm.merchant_id = m.merchant_id
    AND site_id != 1) as isTjoos,
    (select (case when count(*)> 0 then 'No' else 'Yes' end) as result
    from site_merchant sm
    WHERE sm.merchant_id = m.merchant_id
    AND site_id != 2) as isUCOnly,
    (select (case when count(*)> 0 then 'No' else 'Yes' end) as result
    from site_merchant sm
    WHERE sm.merchant_id = m.merchant_id
    AND site_id != 3) as isDLKonly
FROM merchant m
LEFT JOIN merchant_editor_assignment mea ON mea.merchant_id = m.merchant_id
LEFT JOIN user_table ut ON ut.user_id = mea.user_id
LEFT JOIN affiliate a ON a.affiliate_id = m.affiliate_id_default

感谢您的帮助。