is there an SQL command to delete the first X lines of a database table?
I have a database table containing some information but no id or auto-incrementing value and a program that processes the first X lines of this table.
Afterwards these X lines need to be deleted. So the standard query is:
DELETE FROM table WHERE something = value;
So, is there a way to build a query like:
DELETE FROM table WHERE rownumber <= X;
I have tried this command, but nothing happens to the database..
Do you have any clue?
Use LIMIT on your delete:
DELETE FROM table WHERE condition LIMIT 10
Or, if you don't want the condition
DELETE FROM table LIMIT 10
Remember that the order in which rows will be deleted is undefined - it depends on your DBMS configuration and table indices. You should include an ORDER BY so that the deletion is done in a defined order, e.g. ORDER BY id ASC to delete the lowest IDs first.
See the MySQL documentation for DELETE for more details.
Related
Is it possible in sql to delete since one row until the end of the table ?
for instance :
delete from mytable where oneDate ='2017-06-06';
and since this row delete all following rows?
Yes it is.
Example:
DELETE FROM [table] WHERE [table.ID]>=50
Assuming the table has 100 records it would delete from 50 to 100, keeping the first 50. Same with dates and having into consideration some criteria to filter from some specific point on (in my example, the ID).
I have a database called 'master_database' and a table called 'info'
In the 'info' table I have multiple records and I need the 'email' field to not contain any duplicates but currently it does. What SQL command can I run to remove these duplicates?
You can know the rows that are repeated by using this:
SELECT email, COUNT(email) FROM info GROUP BY email HAVING COUNT(email) > 1
DELETE FROM master_database.info WHERE info.ID NOT IN (SELECT MAX(info.ID) FROM master_database.info GROUP BY info.email HAVING COUNT(info.email) > 1)
This assumes you have a unique ID in the table where the higher the number the later the record, if you have a last_edited timestamp it might be better to use the MAX of that.
PLEASE TEST FIRST! Run the following to test:
SELECT * FROM master_database.info WHERE info.ID NOT IN (SELECT MAX(info.ID) FROM master_database.info GROUP BY info.email HAVING COUNT(info.email) > 1)
These values will be deleted.
If you don't have a unique field at all (ie the rows are really duplicates, the following methods will work):
Go to relevant table.
Create select query to select only the relevant duplicate row pairs.
Amend query to delete but add limit like:
DELETE FROM [table name] WHERE [fieldname]LIKE '[value]' AND [fieldname]LIKE '[value]' LIMIT 1
simulate to check effect before using query to delete one of a duplicate pair or repeat usage if there are more than a pair of duplicates
or
Go to relevant table.
Create select query to select only the relevant rows.
Copy query for later pasting.
Export button at the bottom of the query results will export just the query results.
Paste query back but amend it to delete those rows.
Copy export insert query for one of the duplicate rows to reinsert without the duplicate.
I want to delete the most recent entry in MySQL using PHP, is there a function that allows me to do that?
$query2 = "DELETE FROM test WHERE _________";
Assuming you have an AUTO_INCREMENT field on your table, you can do this:
DELETE FROM test WHERE test_id = LAST_INSERT_ID()
You need a datetime column which saved the current time during the INSERT INTO query. Then you can use the ORDER BY and LIMIT keywords to delete the last entry in your table.
DELETE FROM
tab
ORDER BY
datecolumn DESC
LIMIT 1
Check the MySQL 5.1 Reference Manual - 12.2.2 DELETE Syntax for the following statement:
If the DELETE statement includes an ORDER BY clause, rows are deleted in the order specified by the clause. This is useful primarily in conjunction with LIMIT.
DELETE FROM table WHERE id = MAX(id) LIMIT 1
should also be working if you are using auto increment
Here is what im trying to do explained in a query
DELETE FROM table ORDER BY dateRegistered DESC LIMIT 1000 *
I want to run such query in a script which i have already designed. Every time it finds older records that are 1001th record or above it deletes
So kinda of setting Max Row size but deleting all the older records.
Actually is there a way to set that up in the CREATE statement.
Therefore: If i have 9023 rows in the database, when i run that query it should delete 8023 rows and leave me with 1000
If you have a unique ID for rows here is the theoretically correct way, but it is not very efficient (not even if you have an index on the dateRegistered column):
DELETE FROM table
WHERE id NOT IN (
SELECT id FROM table
ORDER BY dateRegistered DESC
LIMIT 1000
)
I think you would be better off by limiting the DELETE directly by date instead of number of rows.
I don't think there is a way to set that up in the CREATE TABLE statement, at least not a portable one.
The only way that immediately occurs to me for this exact job is to do it manually.
First, get a lock on the table. You don't want the row count changing while you're doing this. (If a lock is not practical for your app, you'll have to work out a more clever queuing system rather than using this method.)
Next, get current row count:
SELECT count(*) FROM table
Once you have that, you should with simple maths be able to figure out how many rows need deleting. Let's say it said 1005 - you need to delete 5 rows.
DELETE FROM table ORDER BY dateRegistered ASC LIMIT 5
Now, unlock the table.
If a lock isn't practical for your scenario, you'll have to be a bit more clever - for example, select the unique ID of all the rows that need deleting, and queue them for gradual deletion. I'll let you work that out yourself :)
Is there a way to remove all repeat rows from a MySQL database?
A couple of years ago, someone requested a way to delete duplicates. Subselects make it possible with a query like this in MySQL 4.1:
DELETE FROM some_table WHERE primaryKey NOT IN
(SELECT MIN(primaryKey) FROM some_table GROUP BY some_column)
Of course, you can use MAX(primaryKey) as well if you want to keep the newest record with the duplicate value instead of the oldest record with the duplicate value.
To understand how this works, look at the output of this query:
SELECT some_column, MIN(primaryKey) FROM some_table GROUP BY some_column
As you can see, this query returns the primary key for the first record containing each value of some_column. Logically, then, any key value NOT found in this result set must be a duplicate, and therefore it should be deleted.
These questions / answers might interest you :
How to delete duplicate records in mysql database?
How to delete Duplicates in MySQL table.
And idea that's often used when you are working with a big table is to :
Create a new table
Insert into that table the unique records (i.e. only one version of the duplicates in the original table, generally using a select distinct)
and use that new table in your application ; or drop the old table and rename the new one to the old name.
Good thing with this principle is you have the possibility to verify what's in the new table before dropping the old one -- always nice to check that sort of thing ^^