Mysql

選擇案例查詢

  • July 16, 2017
SELECT  * CASE WHEN (Employees.End_Date is null) THEN 
select  EmpId, Emp_Name, Salary , Start_date , End_date ,
DATEDIFF(DATE_ADD(Start_Date, INTERVAL 30 DAY), Start_Date) * Salary/30 as 'Total_Salary'
   from  Employees ELSE 
Select  EmpId, Emp_Name, Salary , Start_date , End_date ,
DATEDIFF(End_Date, Start_Date) * Salary/30 as 'Total_Salary'
   from  Employees END
   From  Employees;

請幫我找出上述查詢中的語法錯誤。我是 MySql 的新手,所以請幫幫我。

也許像…

select
 EmpId
, Emp_Name
, Salary 
, Start_date 
, End_date   
, case
   when end_date is null then
     DATEDIFF( DATE_ADD(Start_Date, INTERVAL 30 DAY), Start_Date ) * Salary/30 
   else 
     DATEDIFF(End_Date, Start_Date) * Salary/30 
 end as 'Total_Salary'
from employees;

帶有CASE表達式的列可以簡化為:

DATEDIFF(COALESCE(end_date, 
   DATE_ADD(start_date, INTERVAL 30 DAY), start_date) * salary / 30 AS Total_Salary
SELECT * CASE ...

–>

SELECT *, CASE ...

子查詢需要括號:

... THEN SELECT ...

–>

... THEN ( SELECT ... )

您不能在 CASE 中使用子查詢來提供多於一列的一行。

(可能會有更多錯誤。)

引用自:https://dba.stackexchange.com/questions/179939