Is there Any Quick SQL Query to Remove Duplicate Rows [duplicate] - php

This question already has answers here:
MySQL remove duplicates from big database quick
(9 answers)
Closed 8 years ago.
I am trying to remove duplicate rows from my database so for that I am using this query
DELETE FROM data
WHERE data.ID NOT IN (
SELECT * FROM (
SELECT MIN(ID) FROM data GROUP BY Link
) AS p
)
It is working fine but the problem is my database has over 1 Million rows so when I use this it takes the hell of time like after 4 to 5 hours it was still at loading.. and then I just closed the tab.
So Please if someone has a better query tell me. Thanks in Advace
Table Structure
http://s29.postimg.org/bt57k5enb/image.jpg

One solution could be:
1) Create a temp table
2) Store single record for each Link column
3) Truncate "data" table
4) Alter the "data" table(add UNIQUE KEY CONSTRAINT)
5) Reimport data table back from temp table and delete tmp table
1&2) CREATE TABLE tmp AS SELECT * FROM data GROUP BY Link;
3) TRUNCATE TABLE data; -- disable foreign key constraints if any
4) ALTER TABLE data ADD UNIQUE KEY data_link_unique(Link);
5) INSERT INTO data SELECT * FROM tmp;

Related

Fixing unindexed database MySQL [duplicate]

This question already has answers here:
Delete duplicated records from a table without pk or id or unique columns in mysql
(4 answers)
Closed 7 years ago.
EDIT: I added a response with the link and command that solved my issue. To put it straight for any future readers desperate about their unindexed tables with duplicate rows, you'll find the answer bellow or in the link above.
I have a little screwed up table in my db. It was repeated row with the exact same information (including id).
Is there a query I can use to delete these repeated id's but one?
I wanted to check my answer here first, if I delete this table it's going to take me the whole weekend to import it again (it's huge)...
I'm thinking of doing
DELETE FROM tickets t1, tickets t2 WHERE t1.id = t2.id limit 1;
I'm not sure this will work...
Thanks!
As Oceans says
CREATE TEMPORARY TABLE tmp_tickets AS SELECT DISTINCT * FROM tickets;
DELETE FROM tickets;
INSERT INTO tickets SELECT * FROM tmp_tickets;
DROP TABLE tmp_tickets;
To find all the duplicates in the id column you could try:
select `id`
from `tickets`
group by `id`
having count(`id`)>1;
I would try this in a gui ( Heidi etc ) ~ you can then base your delete operations around this, either manually in the gui or by direct query.
I managed to fix this with the solution found here Remove duplicate rows in MySQL. To save you clicking the link the query looked something like this
ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (site_id, title, company);
It removed all the repeated indexes, minus one.

MySql: How to Delete rows from a table EXCEPT a row with specific ID [duplicate]

This question already has answers here:
SQL WHERE condition is not equal to?
(10 answers)
Closed 2 years ago.
I have an html table with checkboxes. Users are permitted to delete rows from that html table except one specific one, whose value is hard coded in the DB.
If a user accidently checks it regardless, I want my server code (php) or even better the MySqL to run a delete query for all the rows they checked EXCEPT for that 1 specific row. So,something like this( which is wrong):
DELETE FROM table_name
WHERE row_id = some_value
EXCEPT row_id = some_value;
Many thanks !
I think the logic you want is AND:
DELETE FROM table_name
WHERE row_id = some_value AND row_id <> some_value;
Of course, some_value should be different most of the time.
DELETE FROM table_name WHERE row_id NOT IN (do_not_delete_this_value );
do_not_delete_this_value will be the id of the row you don't want to delete, rest all records will get deleted.
In fact there are a couple of ways to do it, for deleting all row's except one specific ID, type the following command in SQL query:
In between parentheses, type the ID number that you want to keep.
DELETE FROM `table_name` WHERE `ID` NOT IN (1)
Also, to delete all records except some id's, use this command:
DELETE FROM `table_name` WHERE `ID` NOT IN (1,2,6,10)

How do I delete duplicate rows in SQL? [duplicate]

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I am trying to delete duplicate rows using SQL, whilst leaving one of the rows behind.
The table I am trying to delete duplicate rows from is called table "A" made up of:
A.AID, A.BID, A.AName, A.AType, A.APrice.
In this table I have a number of duplicate rows with all of the data exactly the same apart from the A.ID.
I am trying to create a query that will look for duplicates and then remove the duplicate making sure one of the rows are left behind. I am using phpMyAdmin and MySQL.
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);
use group by and count
DELETE FROM
YourTable
WHERE
AID NOT IN ( SELECT
MAX(AID)
FROM
YourTable
GROUP BY
BID ,
AName ,
AType ,
APrice );
Consider the query below, this will remove all the duplicate rows and prevents any future duplicate row.
ALTER IGNORE TABLE A ADD UNIQUE INDEX index_name (A.AID, A.BID, A.AName, A.AType, A.APrice );

What faster "Insert ignore" or "Select and Insert" [duplicate]

This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux

INSERT with SELECT in MySQL + PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the id of a row i've just inserted php/mysql
I was wondering what's the most efficient way of inserting something and selecting it's ID property at the same time?
For example, I have a table with auto incrementing primary ID column. I insert an item into that table and I need to know the autoincremented ID for use with another table.
Right now, what I do is:
INSERT INTO table1 the data
SELECT FROM table1 the ID
INSERT INTO table2 another set of data along with ID.
You can do this
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); --generate ID by inserting NULL
INSERT INTO foo2 (id,text)
VALUES(LAST_INSERT_ID(),'text'); --use ID in second table
src: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
You can try mysql_insert_id();
http://php.net/manual/en/function.mysql-insert-id.php

Categories