在mysql的单个查询中获取行数

时间:2021-06-11 15:41:49

I have a table in that I want to get the rowcount of records in each column. where column1 is not null column2 is not null column3 is notnull and uID='2'

我有一个表,我想在每列中获取行记录。其中column1不为null column2不为null column3为notnull且uID ='2'

if the record is like

如果记录是这样的

uid   C1    C2    C3
2    Null   3     3
2    2      2     Null

the count here is C1=1, c2=2,c3=1 How can I do this in one query

这里的计数是C1 = 1,c2 = 2,c3 = 1如何在一个查询中执行此操作

4 个解决方案

#1


2  

COUNT(colname) should ignore NULL values in the aggregate, so the query should actually be simple. Note that COUNT(*) behaves differently, and does not ignore NULL rows.

COUNT(colname)应忽略聚合中的NULL值,因此查询实际上应该很简单。请注意,COUNT(*)的行为不同,并且不会忽略NULL行。

SELECT COUNT(C1), COUNT(C2), COUNT(C3) FROM table WHERE uid=2

More information on the NULL aggregate behaviors is found here in the docs.

有关NULL聚合行为的更多信息,请参阅文档。

#2


0  

SELECT COUNT(C1), COUNT(C2), COUNT(C3)
    FROM YourTable
    WHERE uID = 2
    GROUP BY uID

#3


0  

SELECT COUNT(C1), COUNT(C2), COUNT(C3)
FROM Mytable 
WHERE uid=2

By default, it won't count NULL value.

默认情况下,它不会计算NULL值。

#4


0  

select count(C1), count(C2), count(C3) from table

从表中选择计数(C1),计数(C2),计数(C3)

#1


2  

COUNT(colname) should ignore NULL values in the aggregate, so the query should actually be simple. Note that COUNT(*) behaves differently, and does not ignore NULL rows.

COUNT(colname)应忽略聚合中的NULL值,因此查询实际上应该很简单。请注意,COUNT(*)的行为不同,并且不会忽略NULL行。

SELECT COUNT(C1), COUNT(C2), COUNT(C3) FROM table WHERE uid=2

More information on the NULL aggregate behaviors is found here in the docs.

有关NULL聚合行为的更多信息,请参阅文档。

#2


0  

SELECT COUNT(C1), COUNT(C2), COUNT(C3)
    FROM YourTable
    WHERE uID = 2
    GROUP BY uID

#3


0  

SELECT COUNT(C1), COUNT(C2), COUNT(C3)
FROM Mytable 
WHERE uid=2

By default, it won't count NULL value.

默认情况下,它不会计算NULL值。

#4


0  

select count(C1), count(C2), count(C3) from table

从表中选择计数(C1),计数(C2),计数(C3)