Hey so I have one set of data with the structure:
id product_number product_type
1 1001 car
2 1002 house
But the data has some duplicates where:
id product_number product_type
1 1001 car
2 1001 house
I need to delete the duplicates but only the value which is = house.
In my mind the query should be like:
DELETE *
FROM table
WHERE product_number is duplicate AND product_type = house
Thanks
In MySQL, you can do what you want with a join:
delete t
from table t join
(select product, count(*) as cnt
from table
group by product
) tt
on tt.product = t.product
where tt.cnt > 1 and t.product_type = 'house';
You have multiple methods:
1.
You can use this query:
INSERT INTO new_table (SELECT DISTINCT * FROM old_table WHERE product_type = house)
2.
You can alter your table and change your product number column to add an index to it
ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (product_number);
So the duplicated row with the same product number will be dropped automatically, and for more see this link here
3.
You can try this query to:
DELETE n1 FROM table_product p1, table_product p2 WHERE p1.product_number = p2.product_number AND p1.product_type= house
You can use an UPDATE query with a join like this:
delete t1.*
from
t t1 INNER JOIN t t2
ON t1.product_number = t2.product_number
AND t1.product_type='house' AND (
t2.product_type<>'house'
OR t1.id>t2.id
)
DELETE *
FROM table
WHERE id not in
(select max(id) from table
group by product_number)
AND product_type = house
Related
I have two tables
Table 1
ID SELECTEDNAME
101 ______________
Table 2
ID NAME UPDATEDATE
101 JOHN 02-22-17
101 RITA 02-23-17
In Table 1 ID is PK. My requirement is to update the SELECTEDNAME column in Table 1 by extracting the name from Table 2
The name should be extracted from Table 2 such that the corresponding UPDATEDATE should be the latest.
For example, in Table2, I need a query to compare the UPDATEDATE in between the two entries for ID 1 and get the name as RITA as the UPDATEDATE is latest
There could be more than two entries for a given ID in the table.
try like this:
update t1
inner join (select t2.id,max(t2.updatedate) as updatedate
from t2
group by t2.id) t2max on t2max.id=t1.id
inner join t2 on t2.id=t2max.id
SET t1.selectedname=t2.name
WHERE t2.updatedate=t2max.updatedate
DB2 version :
UPDATE table1 f0
SET f0.SELECTEDNAME=
(
SELECT f1.name
FROM table2 f1 INNER JOIN
(
SELECT f2.id, max(f2.updatedate) maxdate
FROM table2 f2
GROUP BY f2.id
) f3 ON (f1.id, f1.updatedate)=(f3.id, f3.maxdate)
WHERE f1.id=f0.id
FETCH FIRST ROWS ONLY
)
WHERE EXISTS
(
SELECT * FROM table2 f1
WHERE f1.id=f0.id
)
Probably is something simple, but, let's say I have these:
Table User (id_user, name)
Table A (id_a, name, type, #id_user)
And then I have another one that have only its own id and the other foreign keys
Table B (id_b, #id_user1, #id_user2, #id_a, #id_Something)
So, I need a query that returns ONLY the rows of table A and table B with what they have in common. I've tried INNER JOIN but it returns all rows of Table A where the id_user from there is equal to the id_user from table B. Like, if I have these:
Table User:
id_user name
1 Hey
Table A:
id_a name type id_user
1 a car 1
2 b cat 1
Table B:
id_b id_user id_user2 id_a id_Something
1 1 Doesn't matter 1 Doesn't matter
I need to return only the common row between Table A and Table B (that'll be something like:
id_a name type id_user id_b id_user2
1 a car 1 1
I've tried INNER JOIN but it returns to me everything when the id_user from A = id_user from B. I used this syntax:
SELECT *
FROM B
INNER JOIN A ON A.id_user = B.id_user;
Hope I've made myself clear, thank you a lot.
Is what you're going after: "Show me all the rows in A and B which share the same id_user"
SELECT User.id_user, User.name, a.id_a, b.id_b
FROM User
INNER JOIN A ON a.id_user = User.id_user
INNER JOIN B on b.id_user = User.id_user
I have two tables. In table one (table_1) I have id, category_name and category_id. Here only id is set. In another one (table_2) I have article_id, content_id and tag_name.
Now from table_1 and table_2 I need to put content_id and tag_name from second into first table category_id and category_name. This can be done like this:
UPDATE table_1
INNER JOIN table_2 ON table_2.article_id = table_1.id
SET table_1.category_id = table_2.content_id
UPDATE table_1
INNER JOIN table_2 ON table_2.article_id = table_1.id
SET table_1.category_name = table_2.tag_name
I tried adding AND and add table_1.category_name = table_2.tag_name, but I didn't get anything so I split the two queries.
But the issue I have is that in my second table, I can have multiple content_id's attached to the same article_id (multiple categories in same article), and with the above I'll only get one integer put in the category_id for an article in the first table.
table_2 for instance:
article_id content_id tag_name
================================
21596 156 Tag Name
21596 16 Second
26189 156 Tag Name
How can I update values from one table to another if there is no one-to-one correspondence? Also, can these values be comma separated?
CLARIFICATION
What I need is a way to pull both tag_names and content_ids from the second table to first table and have something like
table_1:
id category_id category_name
=========================================
21596 156, 16 Tag Name, Second
26189 156 Tag Name
UPDATE
Ok, so I found a way to get all the info concatenated into one from second table, I just need to incorporate this somehow into my update query
SELECT article_id, group_concat(tag_name ORDER BY tag_name, content_id) AS tag_name, group_concat(content_id ORDER BY tag_name, content_id) AS tag_id
FROM table_2
GROUP BY article_id
this will work
UPDATE table_1
INNER JOIN table_2 ON table_2.article_id = table_1.id
SET table_1.category_id = table_2.content_id, table_1.category_name = table_2.tag_name
UPDATE
In oracle , we can achieve with the below LISTAGG function which basically concats columns based on group. Mysql does have a counterpart take a look at this Thread
create table temp
(
article number(2),
test varchar2(30)
)
insert into temp values(1, null);
create table temp_table
(
article number(2),
test varchar2(10)
)
insert into temp_table values(1, 'System');
insert into temp_table values(1, 'Source');
update temp t
set t.test = (
select LISTAGG(test) WITHIN GROUP (order by test) from temp_table tt
where tt.article = t.article
group by
tt.article)
Ok, so the answer is doing two updates one for one column, and one for second column (maybe there's a way to merge those two, but so far this works)
UPDATE
table_1
JOIN (SELECT article_id, GROUP_CONCAT(tag_name ORDER BY tag_name, content_id) AS tag_name
FROM table_2
GROUP BY article_id) table_2
ON table_1.id = table_2.article_id
SET table_1.category_name = table_2.content_name
I am updating first table, but before that I'm concatenating the tag_name in second table, so that I get all tags for one article. Then I just set those values in first table.
The same for id works.
I have a table called students and another table called categories. They are related with the category_id row.
My category table has rows:
1 - VIP
2 - Loyal
3 - Neutral
I want to delete every student which is alone in category.
If I have 5 students which are VIP and one which is Loyal.
How to delete the Loyal?
You can do this using a join. Here is one method:
delete s
from students join
(select category_id
from students s
group by category_id
having count(*) = 1
) sc
on s.category_id = sc.category_id;
group by the category_id and select only those categories that have only one student. Get the id from that student and put it in the delete query.
delete from students
where id in
(
select * from
(
select min(id)
from students
group by category_id
having count(*) = 1
) tmp
)
You need another subquery in between, because you can't select from a table you are deleting from at the same time (in MySQL). So you need to build a temp table.
I have a database table with the columns: email, name, id
There are duplicate records with the same email and name (ids may or may not be different)
I want to remove all duplicates and not keep any of them.
Example
email | name | id
-------------------------------------------------
a#b.com | a | 2 --- remove
a#b.com | a | 4 --- remove
b#c.com | b | 3
c#d.com | c | 5
What sql query can I use in phpmyadmin to do this?
You could use EXISTS:
DELETE FROM TableName t1
WHERE EXISTS
(
SELECT 1 FROM TableName t2
WHERE t1.id <> t2.id
AND COALESCE(t1.email,'') = COALESCE(t2.email,'')
AND COALESCE(t1.name,'') = COALESCE(t2.name,'')
)
I've used COALESCE to also delete duplicates if the emails or names are null.
In MySQL, you should do this with a join:
delete t
from example t join
(select email, name
from example
group by email, name
having count(*) > 1
) todelete
on t.email = todelete.email and t.name = todelete.name;
Unfortunately, MySQL does not support simple subqueries on the table being modified in an update or delete statement.
EDIT:
Deleting a lot of records can be a performance bottleneck. To get around this, create a temporary table with the records you want, truncate the original table, and re-insert them.
So, try this:
create temporary table tempt as
select t.*
from example t join
(select email, name
from example
group by email, name
having count(*) = 1
) tokeep
on t.email = tokeep.email and t.name = tokeep.name;
truncate table example;
insert into example
select * from tempt;
Try the select query first to be sure it has reasonable performance and does what you want.
DELETE n1 FROM tablename n1, tablename n2 WHERE n1.email = n2.email