How to delete the first entry of a table

This is a discussion on How to delete the first entry of a table within the MySQL Database forums, part of the Database Forums category; I know that I can read the first row of a table with $db_query = "SELECT * FROM mytable ORDER BY ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-02-2005
Stefan Mueller
 
Posts: n/a
Default How to delete the first entry of a table

I know that I can read the first row of a table with
$db_query = "SELECT * FROM mytable ORDER BY key_mycode ASC LIMIT 0, 1";

But how can I delete this entry (the whole row)?
Stefan


Reply With Quote
  #2 (permalink)  
Old 12-02-2005
Bill Karwin
 
Posts: n/a
Default Re: How to delete the first entry of a table

Stefan Mueller wrote:
> I know that I can read the first row of a table with
> $db_query = "SELECT * FROM mytable ORDER BY key_mycode ASC LIMIT 0, 1";
>
> But how can I delete this entry (the whole row)?


Use the DELETE statement. ;-)

You can use your SELECT query to get the row with the lowest value in
key_mycode (I phrase it that way because database purists get twitchy
when you talk about the "first" record in a table). Then you can use
the value from that query to form the next DELETE statement:

DELETE FROM mytable WHERE key_mycode = ?

Where I have "?" above, put in the value from the key_mycode field
returned by your SELECT query.

In some RDBMS implementations, you could use subqueries like this:

DELETE FROM mytable
WHERE key_mycode = (SELECT MIN(key_mycode) FROM mytable);

But alas, MySQL cannot do that; you can't select from a table in the
same statement that you delete from that table.

Here's a really fancy method, using an outer join in MySQL's extended
syntax for multi-table deletes:

DELETE FROM m1
USING mytable AS m1 LEFT OUTER JOIN mytable AS m2
ON m1.key_mycode > m2.key_mycode
WHERE m2.key_mycode IS NULL;

In other words, "delete from mytable all rows for which there is no row
with a lower value for key_mycode."

Regards,
Bill K.
Reply With Quote
  #3 (permalink)  
Old 12-02-2005
Dikkie Dik
 
Posts: n/a
Default Re: How to delete the first entry of a table

You could simply replace the keyword SELECT with DELETE:

http://dev.mysql.com/doc/refman/4.1/en/delete.html

Bill Karwin wrote:
> Stefan Mueller wrote:
>
>> I know that I can read the first row of a table with
>> $db_query = "SELECT * FROM mytable ORDER BY key_mycode ASC LIMIT 0, 1";
>>
>> But how can I delete this entry (the whole row)?

>
>
> Use the DELETE statement. ;-)
>
> You can use your SELECT query to get the row with the lowest value in
> key_mycode (I phrase it that way because database purists get twitchy
> when you talk about the "first" record in a table). Then you can use
> the value from that query to form the next DELETE statement:
>
> DELETE FROM mytable WHERE key_mycode = ?
>
> Where I have "?" above, put in the value from the key_mycode field
> returned by your SELECT query.
>
> In some RDBMS implementations, you could use subqueries like this:
>
> DELETE FROM mytable
> WHERE key_mycode = (SELECT MIN(key_mycode) FROM mytable);
>
> But alas, MySQL cannot do that; you can't select from a table in the
> same statement that you delete from that table.
>
> Here's a really fancy method, using an outer join in MySQL's extended
> syntax for multi-table deletes:
>
> DELETE FROM m1
> USING mytable AS m1 LEFT OUTER JOIN mytable AS m2
> ON m1.key_mycode > m2.key_mycode
> WHERE m2.key_mycode IS NULL;
>
> In other words, "delete from mytable all rows for which there is no row
> with a lower value for key_mycode."
>
> Regards,
> Bill K.

Reply With Quote
  #4 (permalink)  
Old 12-02-2005
Stefan Mueller
 
Posts: n/a
Default Re: How to delete the first entry of a table

> DELETE FROM mytable WHERE key_mycode = ?

Works perfect, many thanks
Stefan


Reply With Quote
  #5 (permalink)  
Old 12-02-2005
Markus Popp
 
Posts: n/a
Default Re: How to delete the first entry of a table

You can also use LIMIT in a DELETE statement - although deletion usually
works faster if you can specify the ID which uses a primary key.

Markus


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:16 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0