带有多个选择语句的MYSql子查询

时间:2019-06-14 05:58:47

标签: mysql

我有一张看起来像这样的桌子

const SnackbarComponent = (props: SnackbarProps) => {
  const snackbarProps = {
     open: true,
     onClose: handleClose,
     // this condition solves your problem
     ...(props.autoHideDuration && { props.autoHideDuration })
     // or with default value
     // ...(!props.stayOpen && { autoHideDuration: 4000 })
  }

  return <Snackbar {...snakbarProps} />;
};

是否可以仅使用1条select语句以这种方式对其进行分类?

Savings
ID | Name | Type | Amount | Date
1  | Alex | Cash | 100    | 2019-06-10
2  | Nick | CHQ  | 500    | 2019-06-10
3  | Mike | Cash | 700    | 2019-06-10
4  | Luke | CHQ  | 200    | 2019-06-10
5  | Alex | Card | 300    | 2019-06-10
6  | Alex | Card | 100    | 2019-06-10
7  | Luke | Cash | 900    | 2019-06-10
8  | Alex | Cash | 400    | 2019-06-10
9  | Mike | CHQ  | 200    | 2019-06-10

这是我目前的发言

Final Output
Name | Total Amount | Total Cash | Total Chq
Mike | 900          | 700        | 200
Luke | 1100         | 900        | 200
Alex | 500          | 500        | 0
Nick | 500          | 0          | 500

感谢所有帮助。谢谢

2 个答案:

答案 0 :(得分:5)

conditional aggregationcase when expression一起使用

SELECT name, sum(amount) as 'Total Amount',
SUM(case when type = 'Cash' then amount end) as 'Total Cash',
SUM(case when type = 'Chq' then amount end) as 'Total Chq'
FROM savings 
where date = '2019-06-10' group BY name 

答案 1 :(得分:0)

例如:

select 
    Name,
    sum(Amount) 'Total Amount',
    sum(if(Type = 'Cash', Amount, 0)) 'Total Cash',
    sum(if(Type = 'CHQ', Amount, 0)) 'Total Chq'
from savings 
group by Name;