MYSQL在子查询中找到两个计数列的差异

时间:2017-06-13 07:35:54

标签: mysql sql

我的查询是

SELECT t1.date as plan_date, t1.representative_id, count(*) as 
 visits_planned, t2.actual_visits
    FROM s_day_plan t1
    LEFT JOIN(
        select doctor_id,date_of_visit, count(*) as actual_visits
        FROM s_doctor_visits
        where deleted = 0 and user_id = 2 and item_master_id <> 1
        group by date(date_of_visit) 
    ) as t2 on(date(t2.date_of_visit) = t1.date)
WHERE t1.deleted = 0 AND t1.created_by = 2
GROUP BY t1.date

我得到的结果如下: enter image description here

现在我的要求是获得visits_planned - actual_visits

的区别

当我输入此行时:(visits_planned - t2.actual_visits) as difference会出错:未知列t1.visits_planned

4 个答案:

答案 0 :(得分:1)

visit_planned的原因是派生列,在列表列表中不可用:

   SELECT plan_date, (visits_planned-actual_visits) sub_col
   from(
     SELECT t1.date as plan_date, t1.representative_id, count(*) as 
       visits_planned, t2.actual_visits
        FROM s_day_plan t1
        LEFT JOIN(
            select doctor_id,date_of_visit, count(*) as actual_visits
            FROM s_doctor_visits
            where deleted = 0 and user_id = 2 and item_master_id <> 1
            group by date(date_of_visit) 
        ) as t2 on(date(t2.date_of_visit) = t1.date)
      WHERE t1.deleted = 0 AND t1.created_by = 2
      GROUP BY t1.date
   ) t

使其派生表使列表中的所有派生列都可用。

答案 1 :(得分:1)

t1.visit_planned不是表t1的真实列,它只是用于命名计数的标签。如果你想这样做,你可以做到

 select plan_date, representative_id,visits_planned,actual_visits,
 (visits_planned - actual_visits) as difference
 from (
 SELECT t1.date as plan_date, t1.representative_id, count(*) as 
  visits_planned, t2.actual_visits
  FROM s_day_plan t1
  LEFT JOIN(
    select doctor_id,date_of_visit, count(*) as actual_visits
    FROM s_doctor_visits
    where deleted = 0 and user_id = 2 and item_master_id <> 1
    group by date(date_of_visit) 
    ) as t2 on(date(t2.date_of_visit) = t1.date)
   WHERE t1.deleted = 0 AND t1.created_by = 2
    GROUP BY t1.date) as tab 

或者使用您的查询创建一个视图,然后从那里选择

 create view myviewname as
 SELECT t1.date as plan_date, t1.representative_id, count(*) as 
 visits_planned, t2.actual_visits
 FROM s_day_plan t1
 LEFT JOIN(
    select doctor_id,date_of_visit, count(*) as actual_visits
    FROM s_doctor_visits
    where deleted = 0 and user_id = 2 and item_master_id <> 1
    group by date(date_of_visit) 
    ) as t2 on(date(t2.date_of_visit) = t1.date)
   WHERE t1.deleted = 0 AND t1.created_by = 2
   GROUP BY t1.date
 -----
select plan_date, representative_id,visits_planned,actual_visits,
 (visits_planned - actual_visits) as difference
 from myviewname

答案 2 :(得分:0)

创建变量而不是(as actual_visits)

 SELECT t1.date as plan_date, t1.representative_id, count(*) as 
 visits_planned, count(*) -@var1
 FROM s_day_plan t1
 LEFT JOIN(
    select doctor_id,date_of_visit, @var1 := count(*) 
    FROM s_doctor_visits
    where deleted = 0 and user_id = 2 and item_master_id <> 1
    group by date(date_of_visit) 
 ) as t2 on(date(t2.date_of_visit) = t1.date)
 WHERE t1.deleted = 0 AND t1.created_by = 2
 GROUP BY t1.date

答案 3 :(得分:0)

SELECT t1.date as plan_date, t1.representative_id, count(*) as visits_planned, t2.actual_visits, count(*) - t2.actual_visits AS your_difference_answer FROM s_day_plan t1 LEFT JOIN( select doctor_id,date_of_visit, count(*) as actual_visits FROM s_doctor_visits where deleted = 0 and user_id = 2 and item_master_id <> 1 group by date(date_of_visit) ) as t2 on(date(t2.date_of_visit) = t1.date) WHERE t1.deleted = 0 AND t1.created_by = 2 GROUP BY t1.date

您需要这样做,因为vivsits_planned是派生列并且不存在于表中。一旦你给真正的colum count(*) MySQL了解你想要做什么