在MySQL上进行子选择

时间:2013-07-29 07:25:25

标签: mysql select

我不知道这是否可行,但我正在尝试用几个JOINS和WHERE语句进行选择查询。我需要的是一个基于特定WHERE的字段和另一个具有不同WHERE的字段。

当前SQL查询:

SELECT SUM(a.totaltime), b.user_name, MONTHNAME(a.date)
FROM a 
JOIN c on c.id = a.id                                                                    
JOIN b on b.userid = c.userid    
LEFT JOIN d on a.projid = d.projectid                                                                       
LEFT JOIN e on a.account_id = e.salesorderid                                                          
LEFT JOIN f on e.salesorderid = f.salesorderid                                                      
LEFT JOIN g on d.projectid = g.projectid                                                                     
WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '1' OR g.cf_990 = '1') 
group by user_name,MONTHNAME(a.date);

这样可以正常工作,但这次我需要包含另一个字段:

WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '0' OR g.cf_990 = '0') 

非常感谢任何帮助

编辑:

当前输出是:

 total_time        username        MONTHNAME(a.date)
 22.5              admin           April
 21                admin           June
 15                max             April

所以上面是基于f.cf_991 ='1'或者g.cf_990 ='1'

我需要的是:

 total_time        username        MONTHNAME(a.date)        total_time_2  
 22.5              admin           April                    5
 21                admin           June                     9
 15                max             April                    13

total_time_2基于f.cf_991 ='0'或g.cf_990 ='0'。

2 个答案:

答案 0 :(得分:3)

SELECT SUM(CASE WHEN f.cf_991 = '1' OR g.cf_990 = '1' THEN a.totaltime END) as total_time
, b.user_name, MONTHNAME(a.date)
, SUM(CASE WHEN f.cf_991 = '0' OR g.cf_990 = '0' THEN a.totaltime END) as total_time_2
FROM a 
JOIN c on c.id = a.id                                                                    
JOIN b on b.userid = c.userid    
LEFT JOIN d on a.projid = d.projectid                                                                       
LEFT JOIN e on a.account_id = e.salesorderid                                                          
LEFT JOIN f on e.salesorderid = f.salesorderid                                                      
LEFT JOIN g on d.projectid = g.projectid                                                                     
WHERE c.deleted != "1" 
AND YEAR(CURDATE()) = a_column_containing_a_date
AND (
       ( f.cf_991 = '1' OR g.cf_990 = '1') 
     OR
       ( f.cf_991 = '0' OR g.cf_990 = '0')
    )
group by user_name,MONTHNAME(a.date);

答案 1 :(得分:0)

试试这个

SELECT 
SUM(if(f.cf_991 = '1' || g.cf_990 = '1',a.totaltime,0)),
SUM(if(f.cf_991 = '0' || g.cf_990 = '0',a.totaltime,0)),
b.user_name, MONTHNAME(a.date) FROM a 
JOIN c on c.id = a.id 
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid 
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
 WHERE c.deleted != "1" AND YEAR(DATE(NOW()))
and (( f.cf_991 = '1' OR g.cf_990 = '1')  or( f.cf_991 = '0' OR g.cf_990 = '0') )
group by ser_name,MONTHNAME(a.date);