0

I have 4 tables
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)

It gives me null and 0. Tried a lot but didn't got success.
Thanks

1
  • issue 1. aggregate function without group by issue 2. perhaps no matching record with inner join. So better provide some sample data and expected result. Commented May 13, 2015 at 17:04

2 Answers 2

3

we_vehicles.veh_status cannot equal 1 and 0 simultaneously.

or as it was put by a comment below:

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)
;

Note: I am not sure the JOINs you are using are necessarily appropriate for the final result you want, and using a GROUP BY might be appropriate as well.

Sign up to request clarification or add additional context in comments.

5 Comments

so what will be the solution if i want the count of 0 and 1's from the same we_vehicles.veh_status ?
@uueerdo unless you believe this proof... pleacher.com/mp/mhumor/onezero2.html
@shafee WHERE we_vehicles.veh_status in ('1','0') AND we_clients.client_id= 3 OR... WHERE ((we_vehicles.veh_status='1') OR (we_vehicles.veh_status='0')) AND (we_clients.client_id= 3)
@xQbert i want both 0 and 1 with separate as clause, How that can be done.
@Shafee I am not positive what you mean, but see the edit above ...in a moment.
0

According to your table and something you want, I suggest instead of using join use union (and If you want to duplicate rows, use union all)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.