判断型SQL

# 季付账单明细
SELECT 
  cb.`source_contract_id` AS '合同ID',
  cb.`effective_status` AS '账单生效状态',
  cb.bill_term,
  cb.`contract_bill_id`,
  cb.`is_instalment`,
  FROM_UNIXTIME(cb.`statement_time`,'%Y-%m-%d') AS '账单包应缴时间',
  IF (cs.source_type=1,'账单','杂费') AS '账单类型',
  cs.cost_name AS '科目名',
  (CASE cs.statement_subject_type
  WHEN 1 THEN cs.cost_amount
  WHEN 2 THEN cs.cost_amount*-1
  END
  ) AS '科目金额'
FROM contract_bill_detail cd 
INNER JOIN `contract_bill` cb ON cd.contract_bill_id=cb.contract_bill_id
INNER JOIN contract_statement_subject cs ON cd.contract_bill_detail_id=cs.source_id
WHERE cb.`delete_status`=1 AND cb.`source_contract_id`='134508'
ORDER BY cb.`contract_bill_id`;

 

 

1)IF 判断:

IF(expr1,expr2,expr3)

 

2-1)CASE WHEN THEN END 判断:

SELECT 
CASE 字段
WHEN 值1.1 THEN 返回值
WHEN 值1.2 THEN 返回值 
   ELSE 返回值 
END 
AS test

2-2)CASE WHEN THEN END 判断:

SELECT 
CASE 
WHEN 字段=值1.1 THEN 返回值
WHEN 字段=值1.2 THEN 返回值 
   ELSE 返回值 
END 
AS test

 

你可能感兴趣的:(SQL)