我有一个包含以下数据的表。
id start current
1 today True
2 yesterday False
1 Monday False
3 yesterday True
3 Monday False
4 today
4 Tuesday
5 Wednesday True
6 Friday
6 Monday
7 Sunday True
7 Tuesday
我想检查当前列中有多少id包含所有null并打印该计数。
我想到使用group by id并选择id为current的null,但是它没有给出适当的计数。我只想在特定ID的所有行都将current设为null时计数。
答案 0 :(得分:1)
尝试一下:http://sqlfiddle.com/#!9/31f6e/12
int (*)[3] = &a;
答案 1 :(得分:0)
MAX(current)
为NULL的ID。尝试以下查询(将在MySQL中运行):
SELECT COUNT(DISTINCT IF(derived_t.max_current IS NULL,
derived_t.id,
NULL)) AS ids_with_all_null
(
SELECT id, MAX(current) as max_current
FROM your_table
GROUP BY id
) AS derived_t
答案 2 :(得分:0)
您可以使用存在条款。 “查找单个id
的计数,这些current
的行中没有NULL
以外的其他select count(distinct d.id)
from data d
where not exists (
select *
from data d2
where d2.id=d.id and d2.current is not null
)
值”
$location = City::where('slug',$location_id)->where('status','1')->firstOrFail();
请参见SQLFiddle