簡體   English   中英

SQL查詢計數= 0 /空

[英]SQL query count = 0/null

我有下面的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個查詢結果沒有相同的行數。 我怎么解決這個問題?

您正在嘗試這樣做:

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
  1. 用戶名
  2. 用戶的條目數
  3. int值與NULL不同的項的條目數
  4. 缺少Error is的條目數
  5. ERRORsyntax的條目數

PD:也許您想在查詢末尾添加LIMIT 0,20000 我之所以沒有這樣做,是因為我沒有達到目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM