Home  >  Q&A  >  body text

Looking for alternatives to shorten sqlcode

Please, I want to find another way

Sample data

date tomato Telephone Book Pen
2022-05-15 2 2 3 1
2022-05-15 3 3 3 2

I saw this result

date tomato Telephone Book Pen
2022-05-15 5 5 6 3

I use this

insert into sales.copy 
select date, 
       sum(tomato), 
       sum(phone), 
       sum(book), 
       sum(pen) 
from copy 
where date = '2022-05-15';

delete from sales.copy 
where date = '2022-05-15' 
LIMIT 2;

But I want another way to explain this part briefly

'date, sum(tomato), sum(phone)...'

P粉144705065P粉144705065184 days ago287

reply all(1)I'll reply

  • P粉986028039

    P粉9860280392024-04-03 11:46:53

    So just group the results

    select date, 
           sum(tomato), 
           sum(phone),
           sum(book), 
           sum(pen) 
    from copy 
    where date = '2022-05-15' 
    GROUP BY date

    reply
    0
  • Cancelreply