简体   繁体   中英

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

  1. if count(*) < 2 then Level=1 and Discount=10

  2. if count(*) > 3 then Level=3 and Discount=20

  3. if 0 then both 0

How to do this in 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

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 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 .

I also changed the logic slightly so counts of "2" are included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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