检查每个“id”是否没有“true”值

时间:2013-12-03 11:23:48

标签: mysql

我需要找到domain_link_id,它没有任何(is_active = true)值。

enter image description here

我创建了以下查询,但此查询无法正常运行。请您帮我修复此查询?

SELECT distinct domain_link_history.id,domain_link_history.domain_link_id 
FROM domain_link_history
INNER JOIN (
SELECT id,is_active 
FROM domain_link_history 
GROUP BY domain_link_id HAVING count(is_active) > 1) dlh2 
ON domain_link_history.id = dlh2.id 
where domain_link_history.is_active = false

提前谢谢。

干杯,

4 个答案:

答案 0 :(得分:1)

我认为您需要以下内容:

SELECT domain_link_id 
FROM domain_link_history
WHERE is_active <> 'true'
GROUP BY domain_link_id

这将提供所有domain_link_id字段,其中is_active未设置为true。这也只会显示唯一的domain_link_id,因此您不会获得重复的重复内容。

这是获取您想要的信息的最简单方法,只要我了解您的请求

您也可以使用

SELECT domain_link_id 
FROM domain_link_history
WHERE is_active = 'false'
GROUP BY domain_link_id

这是为了防止您在is_active field

中有其他选项,而不仅仅是true和false

答案 1 :(得分:1)

对子查询进行左连接可能吗?

SELECT DISTINCT domain_link_id
FROM domain_link_history a
LEFT OUTER JOIN
(
    SELECT DISTINCT domain_link_id
    FROM domain_link_history
    WHERE is_active = 'true'
) sub1
ON a.domain_link_id = Sub1.domain_link_id
WHERE Sub1.domain_link_id IS NULL

或使用sum的替代方法: -

SELECT domain_link_id, SUM(IF(is_active = 'true', 1, 0)) AS true_count
FROM domain_link_history
GROUP BY domain_link_id
HAVING true_count = 0

(未经测试)

答案 2 :(得分:0)

您是否尝试将SUMCASE表达式一起使用?

SELECT      MIN(id) AS id
            , domain_link_id
FROM        domain_link_history
GROUP BY    domain_link_id
HAVING      SUM(CASE WHEN is_active = true THEN 1 ELSE 0 END) = 0

您还可以尝试使用MINMAX聚合:

SELECT  t1.id
        , t1.domain_link_id
FROM    domain_link_history AS t1
        INNER JOIN (
            SELECT      MIN(id) AS id
                        , domain_link_id
            FROM        domain_link_history
            GROUP BY    domain_link_id
            HAVING      MIN(is_active) = MAX(is_active)
        ) AS q1
            ON  q1.id = t1.id
            AND q1.domain_link_id = t1.domain_link_id
WHERE   q1.domain_link_id = false

上面的子查询将找到is_active完全false或完全true的所有记录,然后从外部查询中选择false条记录

答案 3 :(得分:0)

我修改了Kickstart的查询,现在可以正常运行了。

SELECT domain_link_id, 
SUM(IF(is_active = 'true', 1, 0)) AS false_count , 
Count(is_active) as history_row_count
FROM domain_link_history
GROUP BY domain_link_id
HAVING false_count = history_row_count