2

次のようなテーブルが 1 つあります。

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
4

2 に答える 2

0

これを試して:

SELECT
    model,
    sum(if(status=1,1,0)) as on,
    sum(if(status=0,1,0)) as off
FROM
    SpaceShips
GROUP BY
    model
ORDER BY
    model
于 2013-10-28T12:19:30.987 に答える