Mysql中if的用法

完整实例:

SELECT A.line, A.Station,A.tester, d.Product,d.SN,d.tdate,d.Result,d.Type,
 #用if判断条件为该查询新增了一列
IF (ISNULL(d.Type),NULL,IF (d.Type = 'KGB'AND d.result = 'P'OR d.Type = 'KFB'AND d.result = 'F','P','F')) AS FinalResult
FROM
 #临时表A
(SELECT	* FROM line WHERE line LIKE '%Bay%' AND line. ENABLE = 'Y') A
LEFT JOIN 
 #临时表d
(SELECT c.SN,c.Type,b.Tdate,b.result,b.product,b.TesterName FROM serial_number C
LEFT JOIN mesdata_hist B ON B.sn = C.sn
WHERE DATE_FORMAT(b.tdate,'%Y-%m-%d %H:%M:%s') <= '2019-09-19 09:30:00' AND DATE_FORMAT(
b.tdate,'%Y-%m-%d %H:%M:%s') >= '2019-09-19 07:15:00') d 
 #链接条件
ON A.tester = d.TesterName
 #查询条件
WHERE 1 = 1
ORDER BY ISNULL(FinalResult),FinalResult,A.line,A.Station

if用法解析

`IF`(expr1,expr2,expr3)
#*expr1:判断条件
#*expr2:如果判断条件为真,返回expr2
#*expr3:如果判断条件为假,返回expr3
#例子
IF (d.Type = 'KGB'AND d.result = 'P'OR d.Type = 'KFB'AND d.result = 'F','P','F')
#解释:
如果(Type='KGB'并且result='P')或者(Type='KFB'并且result='F'),返回P,不然就是F

你可能感兴趣的:(学习)