dev-ria Posted October 23, 2012 Share Posted October 23, 2012 hello, i need to count two different fields in one query but I keep getting the count of only the first field FirstExtracted. SELECT artist,title,url,FirstExtracted, COUNT(FirstExtracted) AS FirstCount, COUNT(LastExtracted) AS LastCount, DATE_FORMAT(`FirstExtracted`,'%m-%d') AS Seconddate FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Seconddate ORDER BY DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') DESC LIMIT 20 I need results as $run['Seconddate']."', ".$run['FirstCount'].",".$run['LastCount'] not sure what I'm missing here. Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/ Share on other sites More sharing options...
Christian F. Posted October 23, 2012 Share Posted October 23, 2012 Tried running that query in the MySQL client or in phpMyAdmin? Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387288 Share on other sites More sharing options...
dev-ria Posted October 23, 2012 Author Share Posted October 23, 2012 i ran it on a php page and mysql workbench. they only bring count of FirstExtracted. It makes count of LastExtracted the same as the FirstExtracted. Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387295 Share on other sites More sharing options...
Christian F. Posted October 23, 2012 Share Posted October 23, 2012 You have some example data we could check with? Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387307 Share on other sites More sharing options...
Barand Posted October 23, 2012 Share Posted October 23, 2012 When you do field count, as you are, only those containing NULL are excluded. Zeros and blank strings are included. Since you have those two fields in each record it is not surprising the count is the same for both. Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387313 Share on other sites More sharing options...
akphidelt2007 Posted October 23, 2012 Share Posted October 23, 2012 (edited) You have to rethink your query to figure out what exactly you are trying to count and 99.99% chance you'll have to use a subquery since grouping and counting will yield you the same results for both counts. Edited October 23, 2012 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387339 Share on other sites More sharing options...
pantu Posted October 24, 2012 Share Posted October 24, 2012 Probably like this SELECT artist, title, url, FirstExtracted, DATE_FORMAT(`FirstExtracted`,'%m-%d') AS Seconddate, (SELECT COUNT(FirstExtracted) FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d')) AS FirstCount, (SELECT COUNT(LastExtracted) FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d')) AS LastCount FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Seconddate ORDER BY DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') DESC LIMIT 20 But why do you do a count in combination with a limit...? Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387408 Share on other sites More sharing options...
dev-ria Posted October 24, 2012 Author Share Posted October 24, 2012 thanks for the help guys. pantu the query didnt work as planned. it counted all the records in the field and didnt split them by date. but the query below i put together seems to work except when I call the LastCount in my while() it comes back as Notice: Undefined index: LastCount SELECT LastExtracted,FirstExtracted, COUNT(FirstExtracted) AS FirstCount, DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') UNION ALL SELECT LastExtracted,FirstExtracted, COUNT(LastExtracted) AS LastCount, DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate ORDER BY Firstdate DESC LIMIT 20 the reason for the limit is im creating a graph so i only need the last 20 days. Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387424 Share on other sites More sharing options...
dev-ria Posted October 24, 2012 Author Share Posted October 24, 2012 ugh its calling LastCount (added LastCount to first SELECT) but it still counts the same as first FirstCount. (SELECT LastExtracted,FirstExtracted, COUNT(FirstExtracted) AS FirstCount, COUNT(LastExtracted) AS LastCount, DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d')) UNION ALL (SELECT LastExtracted,FirstExtracted, COUNT(FirstExtracted) AS FirstCount, COUNT(LastExtracted) AS LastCount, DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate FROM results WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate) ORDER BY Firstdate DESC LIMIT 20 Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387435 Share on other sites More sharing options...
dev-ria Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) here is sample of what the data looks like Edited October 24, 2012 by dev-ria Quote Link to comment https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/#findComment-1387441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.