简体   繁体   English

如何在MySQL中使用带有条件的案例

[英]how to use case with condition in mysql

using mysql query how to use case with condition like ( manager_id=2 ). 使用mysql查询如何使用条件为casemanager_id=2 )的情况。

i have three table with column 我有三个表列

user
 - id
 - name
 - dept
 - manager_id
itemone
 - id
 - detail
 - status
 - amount
 - created_by
itemtwo
 - id
 - detail
 - status
 - amount
 - created_by

based on status column able to find draft, pending, approved and rejected . 根据status栏可以找到草稿,待审核,已批准和已拒绝

when my manager_id is 2 then calculate total for pending if not show as 0.00; 当我的manager_id2 ,如果未显示为0.00,则计算pending总数; How to write query for this? 如何为此编写查询?

Sample Select Query: 样本选择查询:

SELECT t1.name, t1.dept, t1.manager_id, t2.draft_total, t2.pending_total, t2.approved_total, t2.rejected_total FROM user t1, 
(
    SELECT uat.created_by, SUM(uat.amount) AS total, SUM(uat.draft) AS draft_total, SUM(uat.pending) AS pending_total, SUM(uat.approved) AS approved_total, SUM(uat.rejected) AS rejected_total FROM 
    (
        SELECT id, detail, status, amount, CASE status WHEN 1 THEN amount ELSE 0 END AS draft, CASE status WHEN 2 THEN amount ELSE 0 END AS pending, CASE status WHEN 3 THEN amount ELSE 0 END AS approved, CASE status WHEN 4 THEN amount ELSE 0 END AS rejected,created_by FROM itemone UNION ALL SELECT id, detail, status, amount, CASE status WHEN 1 THEN amount ELSE 0 END AS draft, CASE status WHEN 2 THEN amount ELSE 0 END AS pending, CASE status WHEN 3 THEN amount ELSE 0 END AS approved, CASE status WHEN 4 THEN amount ELSE 0 END AS rejected,created_by FROM itemtwo
    ) 
    uat GROUP BY uat.created_by
) t2 WHERE t1.id=t2.created_by ORDER BY name ASC;

Find Table Schema in SQLFiddle SQLFiddle中查找表架构

Getting Following Result 获得跟踪结果

 name       | dept  | manager_id    | draft_total   | pending_total | approved_total    | rejected_total
user four   | Y     | 2             | 79.75         | 54.10         | 90.30             | 100.20  
user one    | X     | 1             | 79.75         | 54.10         | 90.30             | 100.20
user two    | X     | 1             | 84.25         | 0.00          | 0.00              | 0.00

Expecting Output: If Manager id is 2 then Result Should be 预期输出:如果Manager id为2,则结果应为

name        | dept  | manager_id    | draft_total   | pending_total | approved_total    | rejected_total
user four   | Y     | 2             | 79.75         | 54.10         | 90.30             | 100.20  
user one    | X     | 1             | 79.75         | 0.00          | 90.30             | 100.20
user two    | X     | 1             | 84.25         | 0.00          | 0.00              | 0.00

Try out this: 试试这个:

SELECT t1.name, t1.dept, t1.manager_id,
sum(case when t2.status=1 then t2.amount else 0 end) AS draft_total, 
sum(case when t2.status=2 and t1.manager_id=2  then t2.amount else 0 end) AS pending_total, 
sum(case when t2.status=2 then t2.amount else 0 end) AS approved_total, 
sum(case when t2.status=4 then t2.amount else 0 end) AS rejected_total
FROM user t1
inner join (
  select * from itemone
  union all 
  select * from itemtwo
) t2 on t1.id=t2.created_by 
group by t1.id
ORDER BY t1.name ASC

Try this one 试试这个

SELECT CASE user.manager_id WHEN 2 THEN SUM(pending) ELSE 0 END AS pending_total FROM ... WHERE ... SELECT CASE user.manager_id何时2 THEN SUM(pending)ELSE 0 END asending_total FROM ... WHERE ...

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

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