嗨,我有像贝娄这样的餐桌测试

viewtime   event   id
----------------------
10           end   101
290         prog   101
20           rew   101


我想要像end + prog-rew这样的输出,即(10 + 290-20)

输出将是:

viewtime   event   id
280         prog   101

最佳答案

一种解决方案是在以下情况下对SUM和CASE使用GROUP BY查询:

SELECT
  id,
  SUM(CASE WHEN event IN ('end', 'prog') THEN viewtime
           WHEN event = 'rew' THEN -viewtime END) AS viewtime
FROM tablename
GROUP BY id

关于mysql - 如何在同一张表中添加两行并减去一行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34241762/

10-16 11:41