Mysql 选择所有子行符合标准的父行
我有两个表格,比率和标准。标准中的父id指的是比率中的id。 我需要选择表criterias中所有子行的比率,其中criteria_1和criteria_2等于NULL。 在下面的示例中,只应选择统一费率 费率 条件Mysql 选择所有子行符合标准的父行,mysql,sql,Mysql,Sql,我有两个表格,比率和标准。标准中的父id指的是比率中的id。 我需要选择表criterias中所有子行的比率,其中criteria_1和criteria_2等于NULL。 在下面的示例中,只应选择统一费率 费率 条件 id | parent_id | criteria_1 | criteria_2 ------------------------------------------------------ 1 | 1 | 523
id | parent_id | criteria_1 | criteria_2
------------------------------------------------------
1 | 1 | 523 | 563
2 | 1 | null | null
3 | 2 | null | null
4 | 2 | null | null
5 | 3 | 777 | null
我尝试了不存在,但它返回任何一个子项有两个空条件的速率我将使用一些聚合函数,其中
具有按parent\u id
分组的
子句
如果每个parent\u id
至少有一个非空值,则使用min
或max
将返回一个数值,但如果只有null
,则使用null
。所以只需要使用一个having min()为null
就可以找到一个只有null
值的parent\u id
select *
from rates r
where id in(
select parent_id
from criterias
group by parent_id
having min(criteria_1) is null
and min(criteria_2) is null
);
或者使用内部连接(如果您愿意)
通过以下方式验证:
create table rates(
id int,
name varchar(20)
);
create table criterias (
id int,
parent_id int,
criteria_1 int null,
criteria_2 int null
);
insert into rates values (1, 'summer rate');
insert into rates values (2, 'flate rate');
insert into rates values (3, 'student rate');
insert into rates values (4, 'old rate');
insert into rates values (5, 'any rate');
insert into criterias values (1, 1, 523, 563);
insert into criterias values (2, 1, null, null);
insert into criterias values (3, 2, null, null);
insert into criterias values (4, 2, null, null);
insert into criterias values (5, 1, 777, null);
insert into criterias values (6, 4, null, null);
insert into criterias values (7, 5, null, null);
insert into criterias values (8, 5, null, null);
/*insert into criterias values (9, 5, 1, null);*/
select *
from rates r
where id in(
select parent_id
from criterias
group by parent_id
having min(criteria_1) is null
and min(criteria_2) is null
);
结果:
id name
2 flate rate
4 old rate
5 any rate
尝试将此
子查询
与内部联接
一起使用
select * from
(select * from rates where name = 'flat rate') t1
inner join
(select * from criterias where coalesce(criteria_1, 0) = 0 and coalesce(criteria_2, 0) = 0) t2
on t2.parent_id = t1.id
请参阅下面的查询,它应该可以工作。您需要比较2个结果集以查找所有空子项的速率
SELECT
a.parent_id
FROM(
SELECT
parent_id,
COUNT(*) AS total_count
FROM criterias c
WHERE c. criteria_1 IS NULL AND c.criteria_2 IS NULL
GROUP BY 1
) a
INNER JOIN (
SELECT
parent_id,
COUNT(*) AS total_count
FROM criterias c
GROUP BY 1
)b ON a.parent_id = b.parent_id AND a.total_count = b.total_count
使用这样的子查询有什么好处吗?把条件放在
where
子句中不是更干净吗?请注意,这可能(也将)返回重复的比率
,其中不需要标准
。当一个子行的标准_1和标准_2等于NULL@user1709251我的错,没有抓住大胆的部分。。。这就行了trick@user1709251更新。验证脚本显示一个注释行,添加该行将从结果中删除any rate
select * from
(select * from rates where name = 'flat rate') t1
inner join
(select * from criterias where coalesce(criteria_1, 0) = 0 and coalesce(criteria_2, 0) = 0) t2
on t2.parent_id = t1.id
SELECT
a.parent_id
FROM(
SELECT
parent_id,
COUNT(*) AS total_count
FROM criterias c
WHERE c. criteria_1 IS NULL AND c.criteria_2 IS NULL
GROUP BY 1
) a
INNER JOIN (
SELECT
parent_id,
COUNT(*) AS total_count
FROM criterias c
GROUP BY 1
)b ON a.parent_id = b.parent_id AND a.total_count = b.total_count