我有一张这样的桌子:
id model status
1 Star 1
2 Star 0
3 Bana 0
4 Ship 1
5 Star 1
6 Ship 1
7 Ship 0
8 Bana 1
9 Bana 1
10 Ship 0
11 Ship 0
我要这个:
Model On Off
Ship 2 3
Star 2 1
Bana 2 1
我试过这个但是太久了,当我有 8000 条记录在桌子上时......
SELECT a.model,
(SELECT count(b.status) lib
FROM SpaceShips b
WHERE a.model=b.model
AND b.status='1'
GROUP BY b.model) ON,
(SELECT count(c.status) blo
FROM SpaceShips c
WHERE a.model=c.model
AND c.status='0'
GROUP BY c.model) OFF
FROM SpaceShips a
WHERE (a.status='0'
OR a.status='1')
GROUP BY model
ORDER BY model