有没有办法实现CASE
语句,其中WHEN
条件的作用类似于WHERE IN ()
?这样的事情就是我想要的:
SELECT
product_id,
count(product_id) as productCount,
CASE category_id
WHEN IN (1,2,3,4,5) THEN 1
WHEN IN (6,7,8,9,10) THEN 2
WHEN IN (11,12,13,14,15) THEN 3
END as parent_id
FROM product
GROUP BY parent_id
背后的动机:尝试在一个查询中执行我需要做的事情,而不是必须取消子选择或PHP FOR
循环。
我在文档中没有看到如何做到这一点。
答案 0 :(得分:1)
做一个
SELECT
product_id,
count(product_id) as productCount,
CASE
WHEN category_id IN (1,2,3,4,5) THEN 1
WHEN category_id IN (6,7,8,9,10) THEN 2
WHEN category_id IN (11,12,13,14,15) THEN 3
END as parent_id
FROM product
GROUP BY parent_id
中的第二部分