Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: last row

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    last row

    I'm trying to get the value of the primary key for the last row in the database.
    The Product_ID field is auto-incremented at every entry and i want to find out what the biggest Product_ID in the table is.

    I'm using this sql code but it doesnt work

    SELECT *
    FROM 'Prods'
    WHERE [ `Product_ID` = last];

    any suggestions?

    thanks

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    SELECT *
    FROM 'Prods'
    ORDER BY Product_ID ASC
    LIMIT 1;

    I think, or it's DESC instead of ASC.

  • #3
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    why do you need that info? You aren't using it to do an insert into another table are you? I'm asking because that is a very unsafe way of doing it.

    By the way you could use max(product_id).

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by guelphdad View Post
    why do you need that info? You aren't using it to do an insert into another table are you? I'm asking because that is a very unsafe way of doing it.

    By the way you could use max(product_id).
    Nightfires way will work, or simply use mysql_insert_id()... http://ua.php.net/mysql_insert_id to get the previous id.


    How unsafe is it? Out of curiousity ...

    I guess if another program ran at the same time, or another instant of the same program, then it would get tangled up .. ?


  •  

    Posting Permissions

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