We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Mysql order by day and time

cntmedia1
cntmedia1 asked
on
Medium Priority
1,071 Views
Last Modified: 2012-05-06
Take a look at the last post I just had...
It's spin off from this

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Frameworks/Q_24148965.html


I am looking to show the number of views per day.

I know I would have to sort by the date I have in the database.

The thing is I would like to also sort by the hour as well....


So a basic one that sorts out the 7 days of the week...
To show which day was the most popular.

Then also to show the top 5 hours of the day that were the most popular.

Thanks!

I posted what my table database (snapshot) looks like in the last post.
include("database_info.php");
 
//-------------------------
 
    mysql_connect($hostname, $mysql_login, $mysql_password) or die(mysql_error());
    mysql_select_db($database) or die(mysql_error());
 
//-------------------------
 
 
$last10 = mysql_query("SELECT * FROM  $tableName ORDER BY vistid DESC LIMIT 10");
 
 
echo "<table border='1'>";
echo "<tr> <th>Visit ID</th> <th>IP</th> </tr>";
 
while($row = mysql_fetch_array( $last10 )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        echo $row['vistid'];
        echo "</td><td>"; 
        echo $row['ip'];
} 
 
echo "</table>";

Open in new window

Comment
Watch Question

Author

Commented:
This is what I was using from my older code...

But I have the table set up different this time... so this isn't going to work.

Just thought this would help people in the right direction

$checkSundayViews = mysql_query
                    (
                     "
                     
                     SELECT
                     COUNT( DAYOFWEEK( `datetime` ) )
                     AS sunday
                     
                        FROM $tableName
                        WHERE (
                        DAYOFWEEK( `datetime` )
                        ) =1
                                             
                     "
            
 
                    );

Open in new window

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
instead of DAYOFWEEK, use DATE_FORMAT() function:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

the format for the "day of the week" is %w, and for the hour (0-23): %H


$checkSundayViews = mysql_query
                    (
                     "
                     
                     SELECT
                     COUNT( DATE_FORMAT( `datetime`, '%w %H' ) )
                     AS sunday
                     
                        FROM $tableName
                        WHERE (
                        DAYOFWEEK( `datetime` )
                        ) =1
                                             
                     "
            
 
                    );

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.