SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select oldest and newest record based on id and timestamp

    I need to select records from a database based on an id number and timestamp

    I can select based on id without a problem, whare I need help is selecting only the records with the first and latest timestamp.

    Example:

    table 1
    id-------data--------timestamp
    1--------xxx---------1111
    1--------xxx---------1112
    1--------xxx---------1113
    2--------xxx---------1111
    2--------xxx---------1112
    2--------xxx---------1113

    I only want to display records:
    1--------xxx---------1111
    1--------xxx---------1113
    2--------xxx---------1111
    2--------xxx---------1113


    My current code displays all the records:
    Code:
     <?php
    $tableid=68;
    echo "<center><strong>$tableid</strong><br><br></center>";
    $query="SELECT * FROM ad_order WHERE cust_id=$tableid";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num)
    {$id=mysql_result($result,$i,"id");
    //////////////////////////
    /////display only the first and last records of table 68
    /////////////////////////
    echo "<center>$id<br></center>";
    $i++;}
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    Code:
    SELECT * FROM table1 
       WHERE timestamp = (SELECT MIN(timestamp) FROM table1) OR  
       timestamp = (SELECT MAX(timestamp) FROM table1);


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
  •