我有以下MySql表,其中包含~19000个条目:
ID USER FIELD1 FIELD2 SOMEINT ERROR
1 name1 null null null missing...
2 name1 value1 value2 3 validated!
3 name1 value3 wrongvalue1 null syntax
4 name2 wrongvalue2 value4 null syntax
etc...................................................................
我想得到一个这样的列表:
USER totalEntries totalValid totalMissing totalSyntax
name1 3 1 1 1
name2 1 0 0 1
etc...................................................................
我对每个列都有一个查询:
select user, count(user) valid from table where
someint is not null group by user limit 0, 20000;
(total valid entries)
select user, count(*) totalEntries from table group by user
limit 0, 20000; (total entries)
select user, count(*) totalMissing from table where field1 is null or
field2 is null group by user limit 0, 20000; (total Missing entrie)
select user, count(*) syntax from table where error like 'syntax%'
group by user limit 0, 20000 (syntaxerror entries)
问题是"分组"不会将count(...)条目列为
USER valid
...
name3 0
因此4个查询结果没有相同的rowcount。我怎么解决这个问题?
答案 0 :(得分:3)
您正在尝试这样做:
SELECT user, COUNT(*) as totalEntries,
SUM(CASE WHEN someint IS NOT NULL THEN 1 ELSE 0 END),
SUM(CASE WHEN field1 IS NULL OR field2 IS NULL THEN 1 ELSE 0 END),
SUM(CASE WHEN error LIKE 'syntax%' THEN 1 ELSE 0 END)
FROM SomeTable
GROUP by user
NULL
Error is
缺少的条目数ERROR
为syntax
PD:也许你想在查询结尾添加LIMIT 0,20000
。我没有这样做,因为我没有达到目的。