Jump to content

Select If Exist Then Place Zero In Place If Not Exist


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.