I have a table with the following columns: id | url | title | company .
Now, I need to delete rows having same url and title.
I want to know if this can be done only using SQL query.
Delete rows with duplicate column value in MySQL
-
- Posts:107
Delete rows with duplicate column value in MySQL
Admin
Posts:805
An easy way to do this is to add a UNIQUE index on the columns with duplicate values, using the ALTER statement.
When you write the ALTER statement, include the IGNORE keyword. Like so:
This will drop all the duplicate rows. The future INSERTs that are duplicates will error out.
Another solution, if you don't want to alter the column properties then you can use the query below.
Or this:
- It is better to make a backup of the table before applying one of these queries.
When you write the ALTER statement, include the IGNORE keyword. Like so:
Code: Select all
ALTER IGNORE TABLE table_name ADD UNIQUE INDEX idx_name (url, title);
Another solution, if you don't want to alter the column properties then you can use the query below.
Code: Select all
DELETE FROM table_name
WHERE id NOT IN (
SELECT * FROM (SELECT MIN(n.id) FROM table_name n GROUP BY n.url, n.title) x
)
Code: Select all
DELETE a FROM table_name AS a, table_name AS b
WHERE
(a.url = b.url OR a.url IS NULL AND b.url IS NULL)
AND (a.title = b.title OR a.title IS NULL AND b.title IS NULL)
AND b.ID > a.ID;
Similar Topics
- GET_id not working in UnLink (delete file)
PHP - MySQL First post
I searching for an hour for a solution; unlink seems not to work with GET idLast post
<?php
$id = (int) $_GET ;
echo $_GET ;
$file = fopen( '.$_GET...
Here is an answer `o god after 2 hours shame on me for this one`
<?php
$file_pointer = $_GET .'.txt';
if (!unlink($file_pointer)) {
echo (...