如何在使用主查询和子查询的条件之间使用

时间:2018-03-23 03:50:59

标签: mysql sql

我在3个表中使用JOIN并获取计数。

  1. trip_details
  2. escort_allocation
  3. cab_allocation
  4. cab_allocation我正在使用Total_Count和escort_allocation我正在使用Escort_Count,例如 trip_details allocationId我正在使用forien key escort_allocation使用内连接我正在计算。 Adhoc_Trip_Count 我从trip_detailscab_allocation开始。 trip_details allocationId我正在使用forien key cab_allocation 使用这个我正在计算,我的查询工作正常。 现在我的问题是我想在两个日期添加BETWEEN 。在trip_details表中我有一个名为tripDate的列。假设我给的日期如2018-03-20 INTO 2018-03-23表示我希望在这两个日期之间获取记录

      

    MYSQL QUERY

    SELECT COUNT(T.tripId) as Escort_Count,
      (
          SELECT COUNT(*) FROM 
          (
              SELECT a.allocationId
              FROM escort_allocation a 
              INNER JOIN cab_allocation c ON a.allocationId = c.allocationId 
              WHERE c.allocationType = 'Adhoc Trip' 
              GROUP BY a.allocationId
          ) AS Ad
    
      ) AS Adhoc_Trip_Count,
      (SELECT COUNT(id) FROM trip_details) as Total_Count
      FROM 
      ( 
          SELECT a.tripId FROM 
          trip_details a 
          INNER JOIN 
          escort_allocation b 
          ON a.allocationId = b.allocationId 
          GROUP BY a.allocationId 
      ) AS T
    

    SQL FIDDLE

0 个答案:

没有答案