如何在mysql中减去2个查询?

时间:2017-03-15 11:51:29

标签: php mysql

我有2个查询。首先是

SELECT CASE WHEN WEEKDAY('2017-03-01') <= 5 THEN 5 -  
WEEKDAY('2017-03-01') ELSE 0 END + CASE WHEN WEEKDAY('2017-03-03') <= 5 THEN
WEEKDAY('2017-03-03') + 1 ELSE 5 END + (DATEDIFF('2017-03-03', 
'2017-03-01') + 1 - (7 - WEEKDAY('2017-03-01')) - (WEEKDAY('2017-03-03') + 
1)) DIV 7 * 5 AS w_d 

该查询的输出是

w_k
 3

,第二个是

  SELECT count(*) FROM attendence WHERE eid=1 and login < ('2017-03-03' + interval 1 day ) AND login >= '2017-03-01' .

该查询计数(*)

的输出
3

我想减去query1 - 2。

1 个答案:

答案 0 :(得分:1)

试试这个

    Select (
    (SELECT CASE WHEN WEEKDAY('2017-03-01') <= 5 THEN 5 - WEEKDAY('2017-03-01') ELSE 0 END + CASE WHEN WEEKDAY('2017-03-03') <= 5 THEN WEEKDAY('2017-03-03') + 1 ELSE 5 END + (DATEDIFF('2017-03-03', '2017-03-01') + 1 - (7 - WEEKDAY('2017-03-01')) - (WEEKDAY('2017-03-03') + 1)) DIV 7 * 5) - 
(SELECT count() FROM attendence WHERE eid=1 and login < ('2017-03-03' + interval 1 day ) AND login >= '2017-03-01')) AS w_d