2013年11月14日 星期四

mysql-case mysql-when




select 
  id,
  case 
    when report_type = 'P' 
    then amount 
    when report_type = 'N' 
    then -amount 
    else null 
from table


SELECT CompanyName, 
    CASE WHEN Country IN ('USA', 'Canada') THEN 'North America'
         WHEN Country = 'Brazil' THEN 'South America'
         ELSE 'Europe' END AS Continent
FROM Suppliers
ORDER BY CompanyName;




select id,
    case report.type
        when 'P' then amount
        when 'N' then -amount
    end as amount
from
    `report`

SELECT 
    id, IF(report.type = 'P', abs(amount), -1*abs(amount)) as amount
FROM  report


create view  `EMPLOYEE_PARASE_DATE` AS  SELECT *,
 (year(curdate())*12+month(curdate()))-(year(startdateofemp)*12+month(startdateofemp))
 as mms,
 case
when day(curdate())>=day(startdateofemp)
  then 0
else 1
  end as dayadj,
 day(curdate()) day_, day(startdateofemp) jday_
 from employee ;

沒有留言:

張貼留言