Jump to content

Select If Exist Then Place Zero In Place If Not Exist


jeger003

Recommended Posts

hello all,

I am using php and mysql to create a query. I want to create a query (without using php if possible) to count certain items and then place zero if there are none.

 

for example

i want to count hours (10am, 11am, 12pm) and number of results per hour for item1 item2.

 

item1 - 10am - 100 results

item1 - 11pm - 50 results

item1 - no results for 12pm - i want mysql to put 0 instead

 

 

item2 - 10am - 100 results

item2 - there are none for 11am - mysql doesnt know that but I want mysql to display 0 (zero)

item2 - 12pm - 200 results

 

my query:

 

 

SELECT DATE_FORMAT(`FDate`,'%H') AS hours,COUNT(Object) AS Ocount, Name, First,

FROM temptable

WHERE Actioned IS NULL

GROUP BY Name,hours

ORDER BY hours,Name DESC;

 

but within my query i want results of 0 as well.

thanks! that was a good start for me. but here is where I am stuck

 


SELECT DATE_FORMAT(`FDate`,'%H') AS hours, COUNT(Object) AS KeyCount, Name
FROM temp
LEFT JOIN ghours ON hour = DATE_FORMAT(FDate,'%H')
WHERE DATE_FORMAT(`FDate`,'%m-%d') = '11-27'
AND Actioned IS NULL
GROUP BY Name, hours
ORDER BY Name, hours DESC

 

it doesnt return the hours of name that dont have results. it only returns those with results

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.