Hi,

For the first question you can make it in two step :

SELECT MIN(id) FROM names;
SELECT * FROM names WHERE id = min ;
SELECT MAX(id) FROM names;
SELECT * FROM names WHERE id = max ;

Regards,

Jocelyn Fournier
PresencePC

----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, April 21, 2002 4:50 AM
Subject: select first or last row


> Hi
>
> MySql Details ---- My server is running MySQL 3.23.45 and I have
> 4.0.0-alpha-nt on my local test machine.
>
>
>
> What is the fastest way to select the first or the last row from a
database?
>
> I have tried queries like this
>
> (id is an auto incrementing primary key)
> select * from names order by id desc limit 1
> select * from names order by id asc limit 1
> select * from names limit 1
>
>
> if I run the following sql queries with the explain command it returns the
> following results
>
> ****************
> table type possible_keys key key_len ref rows Extra
> ----- ---- ------------- --- ------- --- ---- -----
> names index NULL PRIMARY 4 NULL 61368
> *************
>
> No matter what I do, and what indexes I use, I cannot seem to find a way
of
> selecting the last or first row without scanning the whole table.
>
>
> A few times recently I've wished for a new table type, a table that acts
> more like a pipe or stack just to solve this problem.
>
> Thanks
> Pete Kelly
>
>
> Other Items on my MySql wish list.
>
> 1 . It would also be nice to combine a select and a delete command
together
> to form a whole new command, (Retrive data and auto delete the row)
> 2 . Another new command I would like to see is "Show Processlist History",
> basically this would show the last 30 or so queries run on the server.
> 3. I often run queries like this
>
> SELECT id FROM users WHERE name = 'pete'
> if the row exists
> UPDATE users SET visits=visits+1 WHERE name = 'pete'
> else
> INSERT INTO users (name) VALUES ('pete')
>
> It would be nice to make this type of code quicker.
>
>
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail
<[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>
>


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to