【问题标题】:Multiple Select & INNER JOIN Multiple Where clause多选和 INNER JOIN 多 Where 子句
【发布时间】:2015-05-13 17:01:33
【问题描述】:

我有 4 张桌子
we_vehicles_coordinates
we_vehicles
we_drivers
we_clients

SELECT mrk_lat,we_clients.client_id,mrk_lng ,engine_status ,created_date,live_fuel,
                    count(we_drivers.driver_id) as total_drivers,
                    count(we_vehicles.veh_id) as total_veh,
                    count(we_vehicles.veh_status) as run_veh,
                    count(we_vehicles.veh_status) as stop_veh
                    FROM `we_vehicles_coordinates` 
                INNER JOIN we_vehicles ON
                    we_vehicles.veh_id = we_vehicles_coordinates.veh_id
                INNER JOIN we_drivers ON
                    we_drivers.veh_id = we_vehicles.veh_id
                INNER JOIN we_clients ON 
                    we_clients.client_id = we_vehicles.client_id
                    WHERE (we_vehicles.veh_status='1') AND
                    (we_vehicles.veh_status='0') AND
                    (we_clients.client_id= 3)

它给了我null0。 尝试了很多,但没有成功。
谢谢

【问题讨论】:

  • issue 1. aggregate function without group byissue 2. perhaps no matching record with inner join。所以最好提供一些样本数据和预期结果。

标签: php mysql join inner-join


【解决方案1】:

we_vehicles.veh_status 不能同时等于 1 和 0。

或者正如下面的评论所说:

SELECT /* stuff here */
   , SUM(IF(we_vehicles.veh_status = 1, 1, 0) AS run_veh
   , SUM(IF(we_vehicles.veh_status = 0, 1, 0) AS stop_veh
FROM /* more stuff */
WHERE (we_vehicles.veh_status in ('1','0') AND (we_clients.client_id= 3)
;

注意:我不确定您使用的 JOIN 是否一定适合您想要的最终结果,使用 GROUP BY 也可能合适。

【讨论】:

  • 那么如果我想从同一个we_vehicles.veh_status 中计算 0 和 1 的数量,解决方案是什么?
  • @uueerdo 除非你相信这个证明...pleacher.com/mp/mhumor/onezero2.html
  • @shafee WHERE we_vehicles.veh_status in ('1','0') AND we_clients.client_id= 3 或... WHERE ((we_vehicles.veh_status='1') OR (we_vehicles.veh_status='0')) AND (we_clients.client_id= 3)
  • @xQbert 我希望 0 和 1 都带有单独的 as clause,如何做到这一点。
  • @Shafee 我不是很肯定你的意思,但请稍后查看上面的编辑。
【解决方案2】:

根据你的表格和你想要的东西,我建议不要使用join,而是使用union(如果你想复制行,使用union all

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2010-11-04
    • 2012-04-25
    • 1970-01-01
    • 2014-05-25
    • 2015-09-17
    • 2014-02-07
    相关资源
    最近更新 更多