Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT with Different WHERE Conditions

Tags:

sql

select

where

I would like to run a select something like this.

SELECT
        COUNT(*) WHERE switch=0 AND detail=1 AS zeroone
    AND
        COUNT(*) WHERE switch=0 AND detail=2 AS zerotwo
    AND
        COUNT(*) WHERE switch=1 AND detail=1 AS oneone
    AND 
        COUNT(*) WHERE swithc=1 AND detail=2 AS onetwo
  FROM tablename

Is there a way to do this?

like image 926
steventnorris Avatar asked Jan 16 '23 23:01

steventnorris


1 Answers

You can use a CASE Statement in your SELECT to get the results:

SELECT SUM(case when switch=0 AND detail=1 then 1 else 0 end) as zeroone
    , SUM(case when switch=0 AND detail=2 then 1 else 0 end) as zerotwo
    , SUM(case when switch=1 AND detail=1 then 1 else 0 end) as oneone
    , SUM(case when switch=1 AND detail=2 then 1 else 0 end) as onetw
FROM tablename
like image 180
Taryn Avatar answered Jan 24 '23 13:01

Taryn