0

mysql では、次のようなテーブル:

ID 名 値

1 色 ブルー
1 ウエイト 50
1種類のフルーツ
2色レッド
2 重量 40
3 色の黄色  

「タイプ」の名前/特性を持つ個別の ID の数と、そうでない ID の個別の数を取得したいと考えています。最初のもの (そうするもの) は定義されているので簡単ですが、select count(distinct ID) where name <> 'type' - ID 1 を実行すると、他の行/属性があるため、そのカウントの一部になります。 <>「タイプ」。

この例では、distinct count = 'type' の望ましい結果は 1 (ID 1) で、distinct count <> 'type' の場合は 2 (ID の 2 & 3) です。

前もって感謝します。

4

4 に答える 4

0
SELECT id FROM test GROUP BY id HAVING 
CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%'

なしですべてのIDを提供しますTypehttp://sqlfiddle.com/#!2/30837/1

(Concat withは、偶然に,一致していないことを保証します。)XType

その間

SELECT COUNT(id) AS count, GROUP_CONCAT(id) AS ids 
FROM(SELECT id, count(name) as count FROM test 
GROUP BY id HAVING CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%') as temp;

必要な数が得られます:http://sqlfiddle.com/#!2/30837/9

于 2013-10-21T23:38:25.257 に答える
0

これは、SQL クエリに似ています。ユーザーはテーブルに 1 つのレコードを持っていますが、別のレコードは持っていません。

タイプを持つIDを探しているサブクエリでIDがない場所を選択できます

select count(id) from table where id not in (select id from table where name = 'type')
group by id
于 2013-10-21T23:40:25.537 に答える
0

この特定のタスクでは、次を使用できます。

select count(distinct ID) from table
where ID in (select ID from table where name='type') --- this will give you count of IDs where type exists

select count(distinct ID) from table
where ID not in (select ID from table where name='type') -- this will give you count of IDs without type
于 2013-10-21T23:41:52.920 に答える
-1
SELECT CASE
          WHEN Name='Type' THEN 'Type'
          ELSE 'Non-Type'
       END Name
      ,ID
      ,COUNT(ID)
FROM Stuff
GROUP BY
     CASE
          WHEN Name='Type' THEN 'Type'
          ELSE 'Non-Type'
     END 
    ,ID

SQLFiddleを参照してください

于 2013-10-21T23:35:47.507 に答える