[英]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
也可能合適。
根據您的表和您想要的東西,我建議不要使用join
使用union
(如果要復制行,請使用union all
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.