加入多个查询错误

时间:2014-04-19 06:01:46

标签: mysql sql database join

第一次查询

SELECT a.*,
       ifnull(cnt_all,0) total_drivers,
       ifnull(cnt_active,0) active_drivers,
       ifnull(cnt_idle,0) idle_drivers
FROM ta_agent a
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_all
   FROM ta_drivers
   GROUP BY agent_id) cnt ON a.agent_id=cnt.agent_id
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_idle
   FROM ta_drivers
   WHERE last_viewed=0
   GROUP BY agent_id) idle ON a.agent_id=idle.agent_id
LEFT JOIN
  (SELECT agent_id,
          count(*) cnt_active
   FROM ta_drivers
   WHERE last_viewed=1
   GROUP BY agent_id) active ON a.agent_id=active.agent_id

第二次查询

SELECT FROM_UNIXTIME(date_of_registration, '%Y-%m-%d %H:%i:%s') AS user_registeredon
FROM ta_agent a,
     ta_subscription s
WHERE s.agent_id = a.agent_id

当我单独运行时,这两个查询工作正常。我想加入这两个查询。我试图加入这两个查询但我收到此错误Every derived table must have its own alias

我试过这个

select a.*, ifnull(cnt_all,0) total_drivers,ifnull(cnt_active,0) active_drivers, ifnull(cnt_idle,0) idle_drivers
    from ta_agent a left join (select agent_id, count(*) cnt_all
                       from ta_drivers
                       group by agent_id) cnt on a.agent_id=cnt.agent_id

    left join (select agent_id, count(*) cnt_idle
                       from ta_drivers
                       where last_viewed=0
                       group by agent_id) idle on a.agent_id=idle.agent_id

    left join (select agent_id, count(*) cnt_active
                       from ta_drivers
                       where last_viewed=1
                       group by agent_id) active on a.agent_id=active.agent_id

    left join(SELECT FROM_UNIXTIME( date_of_registration, '%Y-%m-%d %H:%i:%s' ) AS user_registeredon FROM ta_subscription WHERE agent_id = a.agent_id)

我不认为这是获得结果的正确方法。

2 个答案:

答案 0 :(得分:1)

请说明你想如何加入这两个查询.. 寻找类似下面的东西:

SELECT *
FROM
  (SELECT a.*,
          ifnull(cnt_all,0) total_drivers,
          ifnull(cnt_active,0) active_drivers,
          ifnull(cnt_idle,0) idle_drivers
   FROM ta_agent a
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_all
      FROM ta_drivers
      GROUP BY agent_id) cnt ON a.agent_id=cnt.agent_id
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_idle
      FROM ta_drivers
      WHERE last_viewed=0
      GROUP BY agent_id) idle ON a.agent_id=idle.agent_id
   LEFT JOIN
     (SELECT agent_id,
             count(*) cnt_active
      FROM ta_drivers
      WHERE last_viewed=1
      GROUP BY agent_id) active ON a.agent_id=active.agent_id)tempaliasA
LEFT JOIN
  (SELECT FROM_UNIXTIME(date_of_registration, '%Y-%m-%d %H:%i:%s') AS user_registeredon,
          agent_id
   FROM ta_agent a,
        ta_subscription s
   WHERE s.agent_id = a.agent_id)tempaliasB ON tempaliasA.agent_id = tempaliasB.agent_id

答案 1 :(得分:0)

如果你愿意在没有所有这些联接的情况下这样做,那么应该这样做:

SELECT a.*,
  (SELECT COUNT(*) 
    FROM ta_drivers
    WHERE agent_id=a.agent_id
  ) AS total_drivers,
  (SELECT COUNT(*) 
    FROM ta_drivers
    WHERE agent_id=a.agent_id AND lastviewed=1
  ) AS active_drivers,
  (SELECT COUNT(*) 
    FROM ta_drivers
    WHERE agent_id=a.agent_id AND lastviewed=0
  ) AS idle_drivers,
  (SELECT FROM_UNIXTIME(date_of_registration, '%Y-%m-%d %H:%i:%s')
    FROM ta_subscription
    WHERE agent_id=a.agent_i
  ) AS user_registered

我想补充一点,也许您尝试做的问题是,在单个查询中,您使用的是别名“a”......当您将它们组合在一起时,结果查询就不明确了。