いくつかのカテゴリが列に表示される回数を数える必要があります。スポーツ、医療などの文字列として保存され、列名は ct.category_name です。
これは私が適応しているクエリです。すべてのカテゴリ タイプの列が必要です。
select co.order_id, co.catalog_item_id, ct.category_name
from customer_order as co
join item_category as ic on (ic.item_id = co.customer_id )
join category_translations as ct on (ct.category_id = ic.category_id)
where co.paid = 1 and co.customer_id = 22500 and ct.locale = "en"
これをselectステートメントに入れると、すべてがカウントされます。理由はわかりますが、どちらの方向に進むべきかわかりません。
count(CASE
WHEN ct.category_name = "sports" THEN ct.category_name
ELSE 0
end) AS 'sports'
繰り返しますが、各文字列のカウントを独自の列にしたいと思います。どんな助けでも大歓迎です。
私がしようとすると:
select co.order_id, co.catalog_item_id, ct.category_name
, SUM(ct.category_name = "sports") AS `sports`
, SUM(ct.category_name = "medici") AS `medicine`
from customer_order as co
join item_category as ic on (ic.item_id = co.customer_id )
join category_translations as ct on (ct.category_id = ic.category_id)
where co.paid = 1 and co.customer_id = 22500 and ct.locale = "en"
スポーツは2回数えます。いつの場所が間違っていますか?結果:
`23115 271708 sports 483 483`