I have a table named 'Student' with columns 'Name' and 'Roll Number'
name Roll_Number
A 1
A 2
A 1
B 2
B 2
C 3
C 2
D 4
I want to delete all the rows from this table having same name but different Roll_number and insert those rows in new table 'Temp'. So after operation Both tables should be like this
Table Student
name Roll_Number
B 2
B 2
D 4
Table Temp
name Roll_Number
A 1
A 2
A 1
C 3
C 2
Because A and C, both have different values for Roll_Number column, So we delete all the entries of A and C from Student table and add it to Temp Table
So how this can be done through mysql query?
Try something like:
For Student table:
SELECT name, Roll_Number
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) = 1
For Temp table:
SELECT name, Roll_Number
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1
Here is the query that work for me, created from SMA and Tim answers.
For inserting into Temp Table
INSERT INTO Temp (name, Roll_Number)
SELECT name, Roll_Number
FROM Student
WHERE name IN (SELECT name
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1);
Deleting record query is same as mentioned by Tim
DELETE FROM Student
WHERE name IN (SELECT name FROM Temp)
Thanks to both of them
Here is a query to insert all records which, for a given name have more than one Roll_Number, into a new temporary table Temp:
INSERT INTO Temp (name, Roll_Number)
SELECT name, Roll_Number
FROM Student
WHERE name IN (SELECT name
FROM Student
GROUP BY name
HAVING COUNT(DISTINCT Roll_Number) > 1)
And here is a query to delete these same records from Student:
DELETE FROM Student
WHERE name IN (SELECT name FROM Temp)
Note that I did the insertion into the new table first before deleting them from the old table (for obvious reasons).
Related
I have 2 tables and i am joining them using the below Query
Select distinct EmailAddress,CUSTOMER_ID,Send_Date,Unique_key,sub_category
from table1
UNION ALL
Select distinct EmailAddress,CUSTOMER_ID,Send_Date,Unique_key,sub_category
from table2
I am using Unique_key as the primary key. It is the concatination of send date + customer id. Sometimes both the tables can have duplicate keys and hence I want to take only 1row in such cases using the above query
Table 1
EmailAddress CUSTOMER_ID Send_Date Unique_key sub_category
a#gmail.com 1001 07-08-2021 70820211001 chair
Table 2
EmailAddress CUSTOMER_ID Send_Date Unique_key sub_category
a#gmail.com 1001 07-08-2021 7082021100 book
What is expected results ?
EmailAddress CUSTOMER_ID Send_Date Unique_key sub_category
a#gmail.com 1001 07-08-2021 70820211001 chair
Only 1 record should appear in the final table & multiple rows should be skipped. I don't want to change anything in unique key format. Is there any workaround for this?
You need something like:
Select distinct EmailAddress,CUSTOMER_ID,Send_Date,sub_category
from table_1
UNION
SELECT EmailAddress,CUSTOMER_ID,Send_Date,sub_category FROM table_2
WHERE NOT EXISTS ( SELECT NULL
FROM table_1
WHERE table_1.EmailAddress = table_2.EmailAddress ) ;
The below select will return empty set, because you have the WHERE NOT EXISTS condition, return the non matching row.
SELECT EmailAddress,CUSTOMER_ID,Send_Date,sub_category
FROM table_2
WHERE NOT EXISTS ( SELECT NULL
FROM table_1
WHERE table_1.EmailAddress = table_2.EmailAddress
) ;
Demo: https://www.db-fiddle.com/f/pB6b5xrgPKCivFWcpQHsyE/24
Try with your data and let me know.
I have a table with three columns:
customerID (Autoincrement INT)
ShopID (INT)
GroupID (INT)
EmailAddress (varchar)
Password (varchar)
So assuming the GroupID = 2 and I have two shopIDs (25 and 26)
I need to select all records where the customer is in GroupID 2 but in only one of the ShopIDs and insert a new record for that sme customer (with a new customerID but the rest of his other data), so that he can log in to both shops with the same email address and password.
Could anyone help me with how to select all the customerIDs that do not belong to both shops?
You can try using correlated subquery
select customerid from tablename a
where not exists
(select 1 from tablename b where a.customerid=b.customerid
and shopid in (25,26) group by customerid having count(distinct shopid)=2)
i have 2 tables 1st table contain 3 columns id, name and dept_id and 2nd table have 2 columns dept and id i want output in 2 columns 1st is "DEPT_NAME" and 2nd column "NAME" and all name show in 1 row which are related to particular department and name separated by comma like if dept_name is HR then name should be like rick, marsh
select Dept, STUFF((SELECT ','+ Name
FROM Table1 T1
WHERE T1.Dept_Id=T2.ID for xml path('')),1,1,'')Name
FROM Table2 T2
I have a table with products: a foreign key(fk id), name and more data. There are duplicates in name, but there are for example customers related to the fk id
Therefore I want to update the duplicates with the first matching fk id
Example:
fk id name
1 abc
2 abc
3 abc
67 abc
Table after update:
fk id name
1 abc
1 abc
1 abc
1 abc
So far I got a query to put them together in a comma separated list, but I am missing the Update:
SELECT
count(*) as amount,
group_concat(name) as names,
group_concat(id) as ids
FROM db.product
GROUP BY name
HAVING amount> 1;
In mysql you can use joins in update statements. I would create a subquery that returns the lowest id (min()) for each name that appears multiple times. Since in mysql you cannot select from the same table that is being updated, therefore an extra layer of subselect is added on the top of the subselect:
UPDATE db.product
INNER JOIN (SELECT * FROM
(SELECT name, min(id) as minid
FROM db.product
GROUP BY name
HAVING count(*)> 1) t2 ) t on t.name=db.product.name
SET db.product.id=t.minid;
I would like to have a mysql select query that will select all from a table and also include a count of duplicates that exist in the table for a specific field. Here is a table and the results I would like.
TestTable
Id Name Dogs
1 Eric 1
2 Dave 2
3 Chris 4
4 Eric 3
I would like to have the following results returned, based on a search for records that have duplicate names.
Id Name Dogs
1 Eric 1
4 Eric 3
select * from your_table
where name in
(
select name
from your_table
group by name
having count(*) > 1
)
Try This
SELECT
a.Id,a.Name,a.Dogs,
(SELECT count(b.Name)
FROM TestTable b
WHERE b.Name=a.Name) as totcount
FROM TestTable a GROUP By a.Name;
This will work.