Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/269830-counting-two-fields-in-one-query/
Share on other sites

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.

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 by akphidelt2007

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...?

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.

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

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.