MySQL查询乘法,加法和除法

时间:2013-10-08 09:44:38

标签: mysql

我正在尝试制定下面列出的查询;

Select SUM(IF (faultdistribution='crs', 1,0)*8 OR
    IF (faultdistribution='configuration', 1,0)* 6 OR
    IF (faulttype='business' AND faultseverity='fatal', 1,0)* 4 OR
    IF (faulttype='business' AND faultseverity='major', 1,0)* 2 OR
    IF (faulttype='business' AND faultseverity='moderate', 1,0)* 5 OR
    IF (faulttype='business' AND faultseverity='minor', 1,0)* 3 OR
    IF (faulttype='look & feel' AND faultseverity='fatal', 1,0)* 2 OR
    IF (faulttype='look & feel' AND faultseverity='major', 1,0)* 1) as mysum 
    FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'

我打算做的是添加全部或任何一个或两个真实的IF条件。另请访问http://www.sqlfiddle.com/#!2/d2aac/44。需要帮助!

此致

1 个答案:

答案 0 :(得分:2)

Select SUM(IF (faultdistribution='crs', 1,0)*8 +
    IF (faultdistribution='configuration', 1,0)* 6 +
    IF (faulttype='business' AND faultseverity='fatal', 1,0)* 4 +
    IF (faulttype='business' AND faultseverity='major', 1,0)* 2 +
    IF (faulttype='business' AND faultseverity='moderate', 1,0)* 5 +
    IF (faulttype='business' AND faultseverity='minor', 1,0)* 3 +
    IF (faulttype='look & feel' AND faultseverity='fatal', 1,0)* 2 +
    IF (faulttype='look & feel' AND faultseverity='major', 1,0)* 1) as mysum 
FROM tbl_fault 
WHERE product='DAS' 
AND faultdistribution='missed'