I want to remove duplicate lines with the same badge_id and we check the user_id :
For example, with the following data:
id user_id badge_id badge_slot
1 2 ACH_1 0
2 1 ACH_1 0
3 1 ACH_1 0
4 1 AAAAA 0
How to delete ??
If you want to keep the records with the lowest id you can use min()
delete from your_table
where id not in
(
select * from
(
select min(id)
from your_table
group by user_id, badge_id
) x
)
And in MySQL you have the problem that you can't select from the same table that you are deleting from. But you can overcome this by a subselect.
Try this:
DELETE * ,count(*)as n your_table by badge_id HAVING n>1 .
hope it help!
Try this ..
ALTER IGNORE TABLE table ADD UNIQUE KEY idx1('badge_id','user_id');
Related
Consider I have a table structure like this,
id Email
-----------
1 abc#gmail.com
2 abc1#gmail.com
3 abc#gmail.com
4 abc#gmail.com
5 abc#gmail.com
And another table
id userId name
---------------------
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
Now if I want to remove duplicates from table 1, i.e. 2, 3, 4, 5 should be deleted from table1 and its corresponding data in table2.
How would I do this?
Is this possible?
first try to delete duplicate row from user table by keeping the one using below code :
DELETE FROM `user` WHERE `id` NOT IN (select * from ( SELECT MIN(`id`) FROM `user` GROUP BY `email` ) as t)
secondly try to delete other dependent rows from another table by following :
DELETE FROM `user_data` WHERE `u_id` NOT IN (select * from ( SELECT MIN(`id`) FROM `user` GROUP BY `email` ) as t)
hope this will work for you.
In a DML way, try the following query with your table (Edit it according to your tables). NOTE: Delete can be rolled back.
DELETE t1
FROM t1, holdkey
WHERE t1.col1 = holdkey.col1
AND t1.col2 = holdkey.col2
One of my table has a field user_ids and the value of the field like 2,3
group_id| user_ids
--------|------------
1 | 2,3
--------|------------
2 | 5,8
I want to update the field without deleting the current value. For ex. If I need to add 5 for group_id id 1, then 2,3 should be like 2,3,5
I m using this query:
UPDATE users_group SET user_ids = CONCAT( SUBSTRING( user_ids, 1, CHAR_LENGTH( user_ids ) -1 ) , ',5' ) WHERE group_id =1
But it is deleting previous value with comma.
group_id| user_ids
--------|------------
1 | ,5
--------|------------
2 | 5,8
can anyone suggest the right way for this?
Can you not just concatenate it on, rather than trying to split it up first?
UPDATE users_group
SET user_ids = CONCAT_WS(',', user_ids, '5' )
WHERE group_id =1
But this does suggest a badly normalised database design. Generally a comma separated list should instead be stored as rows on another table (ie, one row per value in the list) as suggested by Mark Baker.
EDIT - If you want to only have a single copy of any id in each user_ids field, irrespective of how many times you try to insert it, and you want to be able to add multiple ids at once:-
UPDATE users_group a
INNER JOIN
(
SELECT 3 AS an_id
UNION
SELECT 4
) b
ON FIND_IN_SET(b.an_id, a.user_ids) = 0
SET a.user_ids = CONCAT_WS(',', a.user_ids, b.an_id )
WHERE a.group_id =1
EDIT again - if you have a table of users containing the ids then you can select the ids from that where the id is one of those you want to add.
Something like this.
UPDATE users_group a
INNER JOIN
(
SELECT id
FROM users
WHERE id IN (3, 4)
) b
ON FIND_IN_SET(b.id, a.user_ids) = 0
SET a.user_ids = CONCAT_WS(',', a.user_ids, b.id )
WHERE a.group_id =1
update table1 set name = concat(name, ', ', 5) WHERE group_id =1
Please try this query. It may be useful for you.
UPDATE users_group SET user_ids = CONCAT( user_ids , ',5' ) WHERE group_id =1
Try the below query:
UPDATE users_group
SET user_ids = CONCAT( user_ids , ',5' )
WHERE group_id =1
I want to keep the 10 latest duplicate rows and delete all the others.
I'm using the below code, but it deletes all the records except for one.
DELETE FROM `history` WHERE id NOT IN (SELECT * FROM (SELECT MIN(n.id) FROM history n GROUP BY n.url) x)`
Please try this query
This code will remove all duplicate row who have more then 10 duplicates.
DELETE FROM `history` WHERE id NOT IN (
SELECT * FROM (
SELECT MIN(n.id) FROM history n GROUP BY n.url having count(n.url) > 10
) x
)
Try this code that will work.
DELETE
FROM
history
WHERE
id NOT IN
(SELECT
id
FROM
(
SELECT
#num:=IF(#current=url, #num+1, 1) AS row_num,
#current:=url AS group_url,
id
FROM
history
ORDER BY
id
) AS `internal`
WHERE
row_num<=15
)
I have a mysql table which looks something like this:
id_one id_two
1 2
2 1
3 2
2 3
4 5
5 4
I want to delete rows with two duplicate values inrespective of which columns they are in so the example would look like this:
id_one id_two
1 2
3 2
5 4
There are over 12 million rows in total. Any ideas on how I should do this?
Php or mysql query would be preferred.
DELETE a
FROM table1 a
LEFT JOIN
(
select id_one, id_two
from Table1
GROUP BY least(id_one, id_two), greatest(id_one, id_two)
) b ON a.id_one = b.id_one AND a.id_two = b.id_two
WHERE b.id_two IS NULL
SQLFiddle Demo
I would advise a 2-step approach:
Make id_one always the smaller value, i.e., if id_one is larger than id_two then swap their values - consider something like this (taken from here):
UPDATE tablename
SET id_one = (#temp:=id_one), id_one = id_two, id_two = #temp
WHERE id_one > id_two
Remove the duplicates as described here:
DELETE tablename FROM tablename INNER JOIN
(SELECT min(primary_key) AS min_id, id_one, id_two FROM tablename
GROUP BY id_one, id_two
HAVING count(1) > 1) AS d
ON (d.id_one = tablename.id_one
AND d.id_two = tablename.id_two
AND d.min_id <> tablename.primary_key)
(I assume that you will have a primary key on a table that holds 12 million entries.)
Not tested, so please backup your data!
DELETE FROM ztable zt
WHERE zt.id_one > zt.id_two
AND EXISTS (
SELECT *
FROM ztable tx
WHERE tx.id_one = zt.id_two
AND tx.id_two = zt.id_one
)
;
won't work in mysql, because in mysql you cannot reference the table being updated or deleted.
Since you want to make a backup copy anyway, you could use that instead in the EXISTS subquery:
CREATE table safetable AS (SELECT * from ztable);
DELETE FROM ztable zt
WHERE zt.id_one > zt.id_two
AND EXISTS (
SELECT *
FROM safetable tx
WHERE tx.id_one = zt.id_two
AND tx.id_two = zt.id_one
);
I have a table like this:
id count
23432 0
34242 1
12345 5
32235 20
45645 3
How can i select a ROW column(count) value 20 by the column(id) value 32235?
I think this should be pretty straight-forward.
SELECT `count`
FROM tablename
WHERE id = 32235
SQLFiddle Demo
Use this:
SELECT * FROM table_name WHERE count = 20 && value=32235;