SQLite:使用group-by,count更新列

时间:2017-05-28 22:37:10

标签: sql sqlite group-by

我正在使用SQLite,并试图弄清楚如何为客户提供超过3次购买记录的折扣。我尝试了以下内容,但它只更新了返回的第一行customer_ID组的折扣。

update orders
  set discount = price*0.5
where customer_ID = (
    select customer_ID from orders
    group by customer_ID
    having count(customer_ID) > 3
);

如何实现上述所需结果?

1 个答案:

答案 0 :(得分:1)

您应该使用IN代替=

= 意味着匹配一个等于的结果

IN 表示匹配列表

中包含的所有结果
update orders
  set discount = price*0.5
where customer_ID IN (
    select customer_ID from orders
    group by customer_ID
    having count(customer_ID) > 3
);