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.
Related
This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 3 years ago.
I have a SQL Tabel with three column id, Username, and Video I need a SQL query to remove all duplicates thanks
table SS link https://ibb.co/wC4p7kS
Since your screenshot is of phpMyAdmin, I'm going to assume that we're talking about MySQL here (when asking questions, that's helpful information as various SQL dialects have different tools that can be used).
If you don't need id in your results, you can just use DISTINCT:
SELECT
DISTINCT
Username,
Video
FROM
YourTableName
This returns a list of distinct rows (meaning it considers all columns in determining what is distinct, which is why including id here won't work).
If you do need to return an ID, you would have to use GROUP BY:
SELECT
MIN(id) AS id,
Username,
Video
FROM
YourTableName
GROUP BY
Username,
Video
Obviously from there, you could select a different ID (MAX(id) for example), or you could select a list of IDs using GROUP_CONCAT (e.g. GROUP_CONCAT(id ORDER BY id ASC) AS id, which will give you a comma-separated list by default unless you change it)
It can also be done using window/analytic functions, which can be a value in very large tables.
Note: When you say "remove all duplicates", I'm assuming you mean from your results. If you mean literally delete them from the table, you could use this:
DELETE FROM
YourTableName
WHERE
id NOT IN(SELECT MIN(id) FROM YourTableName GROUP BY Username,Video)
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)
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 );
This question already has an answer here:
Find missing sequence gaps mysql [duplicate]
(1 answer)
Closed 8 years ago.
I'm using MySql to keep track of images, each image is assigned an auto increment ID. The ID column should look in theory similar too,
(1,2,3,4,5,6)
Sadly, over the past two years I've been forced to delete rows due to moderation reasons and my ID column looks more along the lines of,
(1,3,4,7)
Is there anyway to get the missing IDs, i.e.:
(2,5,6)
I've search around here and various other forums, but alas I've come up with nothing. The only answer that i've found similar to mine is comparing my ID column to another table's and finding the differences. This is an option, but my main table consists of 25,000 rows so it would be a bit difficult to do.
Thank you in advance!
Create temporary table with integer from 1 to 25000. Then do a left/right join on it to find the missing IDs.
Try that:
SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
FROM table1 AS a, table1 AS b
WHERE a.id < b.id
GROUP BY a.id
HAVING start < MIN(b.id) ;
DEMO HERE
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;