多个 Select 和 INNER JOIN 多个 Where 子句
Multiple Select & INNER JOIN Multiple Where clause
我有 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)
它给了我 null
和 0
。
尝试了很多但没有成功。
谢谢
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
也可能合适。
根据您的 table 和您想要的东西,我建议不要使用 join
,而是使用 union
(如果您想复制行,请使用 union all
)
我有 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)
它给了我 null
和 0
。
尝试了很多但没有成功。
谢谢
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
也可能合适。
根据您的 table 和您想要的东西,我建议不要使用 join
,而是使用 union
(如果您想复制行,请使用 union all
)