0

私はやりたいselect a count returned by a query when a particular column is nullと別のquery to select a count when that column is not null単一のクエリにどのようにそれを達成することができますか..?

私はSOFで利用できるが役に立たない例のいくつかを試しました。

たとえば、私はしたい

select students count of class table where the address null and notnull
4

4 に答える 4

3

MySQLではこれでできる

SELECT 
    SUM(IF(address IS NULL,1,0))       as  `Student_With_No_Address`,
    SUM(IF(address IS NOT NULL,1,0)) as    `Student_With_Address`
FROM students

SQL フィドルのデモ

出力:

Student_With_No_Address |   Student_With_Address
---------------------------------------------
        4               |           6
于 2013-03-09T06:15:23.437 に答える
2

これを試して

SELECT 
COUNT(CASE when address is null then 1 end) AS StudentsWithNoAddress,
COUNT(CASE when address is not null then 1 end) AS StudentsWithAddress 
FROM Class
于 2013-03-09T06:02:38.030 に答える
1

2 つのSELECTステートメントを記述し、次を使用してそれらを結合する必要があります。UNION

SELECT 'No Address' AS AddressStatus, COUNT(*) AS NoOfStudents 
FROM Class WHERE Address IS NULL
UNION
SELECT 'With Address' AS AddressStatus, COUNT(*) AS NoOfStudents 
FROM Class WHERE Address IS NOT NULL
于 2013-03-09T05:58:59.403 に答える
0
select 
 SUM( CASE when studentId is not NULL THEN 1 else 0 END ) as result1 ,
 SUM( CASE when studentId is NULL THEN 1 else 0 END) as result2
from class
于 2013-03-09T07:02:26.553 に答える