0
SELECT COUNT(*) AS count, state, DATE_FORMAT( '%m/%d', registered ) AS date_formatted
FROM users
WHERE CAST( registered AS DATE ) > DATE_SUB( CURDATE(), INTERVAL 7 DAY)
GROUP BY state, date_formatted

Above is our query we are using to return information from our database.

Our dates are saved as: 2011-03-28 14:36:48 (datetime field)

The data being returned is as follows:

Array
(
    [] => Array
        (
            [act] => 1
            [nsw] => 1
            [nt] => 1
            [qld] => 3
            [sa] => 1
            [tas] => 1
            [vic] => 1
            [wa] => 4
        )

)

And it isn't returning the date in the array.

while( $row = mysql_fetch_array( $query )) { $my_array[$row['date_formatted']][$row['state']] = $row['count']; }

Here is the php code we are using to form the array.

Thanks :)

1
  • 1
    I cannot believe that for query with 3 expressions in SELECT clause you get 8 values in php. It is just impossible. PS: CAST( registered AS DATE ) casting here is pointless and declines the possibility of using indexes. Commented Mar 29, 2011 at 5:42

1 Answer 1

3

DATE_FORMAT takes the date as the first argument, and the format as the second argument:

SELECT COUNT(*) AS count, state, DATE_FORMAT(registered ,'%m/%d')
...
Sign up to request clarification or add additional context in comments.

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.