webForumDet fria alternativet

Tre olika count i en select sats

3 svar · 304 visningar · startad av Addeladde

AddeladdeMedlem sedan jan. 20013 406 inlägg
#1

Hej

Kan man göra en select sats där man får tre olika count.

id typ
1 0
2 0
3 1
4 0
5 2
6 2

Jag vill gå ut tre count:

typ0 = 3
typ1 = 1
typ2 = 2

Går det med en select eller ska jag köra tre select satser?

yohpopsMedlem sedan feb. 20011 198 inlägg
#2

Du kan göra på båda setten beroende på hur du vill att det ska visas.

select 'typ0' as typ,count(typ) as cnt from tabell where typ=0
union
select 'typ1' as typ,count(typ) as cnt from tabell where typ=1
union
select 'typ2' as typ,count(typ) as cnt from tabell where typ=2

eller

select (select count(typ) from tabell where typ=0) as typ0
,(select count(typ) from tabell where typ=1) as typ1
,(select count(typ) from tabell where typ=2) as typ2
@ndersMedlem sedan juni 200032 969 inlägg
#3
select typ, count(*) from tabell
group by typ
AddeladdeMedlem sedan jan. 20013 406 inlägg
#4

Tack!

129 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
126 ms — hämta tråd, inlägg och bilagor (db)