Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COUNT() over result of Except

I have a query like:

SELECT
    id
FROM
    table1
INNER JOIN
    ...
WHERE
    ...
    AND eventtype IN (2000120, 2000121, 2000122, 2000123, 2000130)

EXCEPT

SELECT
    id
FROM
    table1
INNER JOIN
    ...
WHERE
    ...
    AND eventtype IN (2000123,2000130,2000134,2000135)

Note that the queries are both identical except the last where-clause.

I would now like to count the number of records returned by this above query.

How to?

Many thanks

like image 539
Atmocreations Avatar asked Dec 12 '25 05:12

Atmocreations


1 Answers

use COUNT and wrap it inside a SUBQUERY

SELECT COUNT(ID)
FROM (
        SELECT id
        FROM table1
        INNER JOIN...
        WHERE...
                AND eventtype IN ( 2000120, 2000121, 2000122, 2000123, 2000130 )
        EXCEPT
        SELECT id
        FROM table1
        INNER JOIN...
        WHERE...
                AND eventtype IN ( 2000123, 2000130, 2000134, 2000135 )
        ) s
like image 82
John Woo Avatar answered Dec 14 '25 18:12

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!