多选和INNER JOIN Multiple Where子句

时间:2015-05-13 17:01:33

标签: php mysql join inner-join

我有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。 尝试了很多,但没有成功 谢谢

2 个答案:

答案 0 :(得分:3)

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

或正如以下评论所述:

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也可能是合适的。

答案 1 :(得分:0)

根据您的表格以及您想要的内容,我建议您不要使用join使用union(如果您想复制行,请使用union all