简体   繁体   English

根据来自另一个表的count(*)值更新表中的多列

[英]Updated multiple column in a table based on the count(*) value from another table

Below is my select Query 以下是我的选择查询

select orders.customerid,count(*) as count 
from Orderitem 
join orders on  OrderItem.orderno = orders.orderno 
group by customerid

I want to update columns 'Level' and 'Discount' in the table 'custtable' based on the count(*) value for the customerid 我想根据customerid的count(*)值更新表“ custtable”中的“级别”和“折扣”列

  1. if count(*) < 2 then Level=1 and Discount=10 如果count(*)<2,则级别= 1,折扣= 10

  2. if count(*) > 3 then Level=3 and Discount=20 如果count(*)> 3,则级别= 3,折扣= 20

  3. if 0 then both 0 如果为0,则均为0

How to do this in Mysql? 如何在Mysql中做到这一点?

As you didn't provide test case, I did it myself. 因为您没有提供测试用例,所以我自己做了。 Might not be perfect, but it's better than none . 可能不是完美的,但总比没有好

SQL> create table custtable (customerid number, c_level number, discount number);

Table created.

SQL> insert into custtable
  2    select 1, null, null from dual union all
  3    select 2, null, null from dual;

2 rows created.

SQL>
SQL> create table orders (customerid number, orderno number);

Table created.

SQL> insert into orders
  2    select 1, 100 from dual union all
  3    select 3, 300 from dual;

2 rows created.

SQL>
SQL> create table orderitem (orderno number);

Table created.

SQL> insert into orderitem
  2    select 100 from dual union all
  3    select 300 from dual;

2 rows created.

This is your query: 这是您的查询:

SQL> select d.customerid, count(*) as count
  2    from orderitem i join orders d on d.orderno = i.orderno
  3    group by d.customerid;

CUSTOMERID      COUNT
---------- ----------
         1          1
         3          1

SQL>

In order to perform update , I'd suggest using MERGE statement, such as 为了执行更新 ,我建议使用MERGE语句,例如

SQL> merge into custtable t
  2    using (select d.customerid, count(*) as cnt
  3           from orderitem i join orders d on d.orderno = i.orderno
  4           group by d.customerid
  5          ) x
  6    on (t.customerid = x.customerid)
  7  when matched then update set
  8    t.c_level = case when x.cnt < 2 then 1
  9                     when x.cnt > 3 then 3
 10                     when x.cnt = 0 then 0
 11                end,
 12    t.discount = case when x.cnt < 2 then 10
 13                      when x.cnt > 3 then 20
 14                      when x.cnt = 0 then 0
 15                 end;

1 row merged.

The result: 结果:

SQL> select * From custtable;

CUSTOMERID    C_LEVEL   DISCOUNT
---------- ---------- ----------
         1          1         10
         2

SQL>

You can do this with a correlated subquery in an UPDATE statement: 您可以在UPDATE语句中使用相关子查询来执行此操作:

update custtable
    set (level, discount) = 
         (select (case when count(*) = 0 then 0
                       when count(*) <= 2 then 1
                       else 3
                  end) as level,
                 (case when count(*) = 0 then 0
                       when count(*) <= 2 then 10
                       else 20
                  end) as discount                  
          from Orderitem oi join
               orders o
               on oi.orderno = o.orderno 
          where o.customerid = custtable.customerId
         );

Note that Oracle lets you update multiple columns at the same time in an update . 注意,Oracle可以让你在在同时更新多个列update

I also changed the logic slightly so counts of "2" are included. 我还略微更改了逻辑,因此包括了“ 2”的计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM