SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    get latest time, then get all rows of that time, in one rather than two queries?

    at the moment i'm doing
    Code:
    $sql = "SELECT my_datetime FROM my_table ORDER BY my_datetime DESC LIMIT 1";
    $result = mysql_query( $sql, $db );
    if( !$result ) { die('Invalid query: ' . mysql_error()); }
    $my_datetime = mysql_fetch_array($result,MYSQL_ASSOC);
    $my_datetime = $my_datetime['my_datetime'];
    to get the most recent datetime in the table. there will be a number of rows with that datetime. to get them i:
    Code:
    $sql = "SELECT * FROM my_datetime WHERE my_datetime = '$my_datetime'";
    ...
    how can i do that with one sql query rather than two?

    thanks.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,492
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Code MySQL:
    SELECT * 
    FROM my_datetime 
    WHERE my_datetime = 
       ( SELECT MAX(my_datetime)
         FROM my_datetime                )

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i see, great, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •