I need to delete duplicate record from table in mysql.
So i have a table name "employee" fields are empid, empname, empssn
for getting duplicate record i have written a query
SELECT COUNT(empssn), empssn FROM employee
GROUP BY empssn
HAVING COUNT(empssn) > 1
Now I want to delete duplicate records. For that I have written query is.
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT MIN(empid), empssn FROM employee
GROUP BY empssn
);
you can assume records in table are
EmpId EmpName EmpSSN
-------------------------------
1 Jack 555-55-5555
2 Joe 555-56-5555
3 Fred 555-57-5555
4 Mike 555-58-5555
5 Cathy 555-59-5555
6 Lisa 555-70-5555
7 Jack 555-55-5555
8 Mike 555-58-5555
9 Cathy 555-59-5555
10 Lisa 555-70-5555
11 Lisa 555-70-5555
but I have a mysql error is
You can't specify target table 'employee' for update in FROM clause
Does the trick of wrapping it into a derived table work for this case? (Based on http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/)
DELETE FROM employee
WHERE (empid, empssn) NOT IN (
SELECT empid, empssn FROM (
SELECT MIN(empid) AS empid, empssn FROM employee
GROUP BY empssn
) X
);
Edit Yep it seems to work this end.
Related
I would like to create a temporary table that extracts the first name and last name of all profiles from the [Person] table in my SQL database. Then, I want to replicate each person 12 times in this new table and add a column called Month so that each of the 12 replicated rows is inserted with a digit (1-12) to represent a month of the year. Can you tell me how to create a SQL for this?
Using MySQL UNION ALL to create a list of each month repeated for each name.
Repeat the select query once for each month, change the month number for each one:
SELECT name, 1 `month` FROM EMPLOYEE
UNION ALL
SELECT name, 2 FROM EMPLOYEE
UNION ALL
SELECT name, 3 FROM EMPLOYEE
ORDER BY month, name
;
Given EMPLOYEE table:
id
name
1
Clark
2
Dave
3
Ava
This outputs:
name
month
Ava
1
Clark
1
Dave
1
Ava
2
Clark
2
Dave
2
Ava
3
Clark
3
Dave
3
-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
-- insert
INSERT INTO EMPLOYEE VALUES (1, 'Clark', 'Sales');
INSERT INTO EMPLOYEE VALUES (2, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE VALUES (3, 'Ava', 'Sales');
INSERT INTO EMPLOYEE VALUES (4, 'Clark', 'Maintenance');
-- fetch
SELECT name, 1 `month` FROM EMPLOYEE
UNION ALL
SELECT name, 2 FROM EMPLOYEE
UNION ALL
SELECT name, 3 FROM EMPLOYEE
ORDER BY month, name
;
Try it here: https://onecompiler.com/mysql/3ygcscr33
Note: UNION ALL is required in case names are the same for multiple people.
I have a table:---
id
name
dept
1
Alice
CS
2
Bob
Elect
3
David
Mech.
and a query result:-
id
count
1
100
2
22
3
50
Then I want to add the count column from the query to my original table, something like:-
id
name
dept
count
1
Alice
CSE
100
2
Bob
Elect
22
3
David
Mech.
50
The only I figured out to do, is by storing the query result into a new table and then using UPDATE...SET...WHERE. Is there any way to do it without creating a new table?
First you need to create the count column in tablename using
ALTER TABLE `tablename` ADD COLUMN `nr_count` INT;
Then use:
update tablename t
inner join ( SELECT id,
count(*) as nr_count
FROM tablename
GROUP BY id
) as t1
set t.nr_count=t1.nr_count ;
I have two tables:
Table 1 (customers)
customer_id customer_name salesPerson_id
1 John 1
2 Ed 1
3 Sam 2
Table 2 (customerContacts)
contact_id customer_id phone_number
1 1 687-5309
2 1 555-1234
3 1 742-1111
I am trying to let only the sales person add / update a phone number for their specific customer.
So only sales salesPerson_id 1 could update John and Ed and only salesPerson_id 2 could update Sam.
I believe I am looking for something like:
INSERT INTO customerContacts (contact_id , customer_id , phone_number) VALUES (1 , 1 , '987-6543')
ON DUPLICATE KEY UPDATE phone_number='987-6543'
IF customers.salesPerson_ID = 1
But it doesn't seem like sql supports if statements.
INSERT INTO customerContacts (contact_id , customer_id , phone_number)
Select 1 , customer_id , '987-6543'
from customers
where salesPerson_ID = 1 and customer_id=1;
This is the query which you should use in the native way but you need to fit this in your application framework
I have two different tables.
Department table
//Department
D# DNAME
-------------------
1 SALES
2 ACCOUNTING
3 GAMES
5 SPORTS
Employee Table
//Employee
E# D#
-----------
1 3
2 2
3 5
4 5
Now using Update statement , update the D#=5 to D#=3;
Currently using this statement
UPDATE EMPLOYEE SET D# = 3 WHERE D# = 5;
But then i trying to learn that if i don't want update using D#, but want to update using the DNAME which mean E# from SPORT will change to GAMES , what should i do to solve it.
You want the multi table update
Something along the lines of:
update employee e
join department d using (d#)
set e.d# = 3
where d.dname = 'SPORTS';
sqlfiddle
I want to run a SP every 10 mins to check if 2 tables, if a value exists in table 1 col1 and not in table 2 col1 then insert a predfined entry into table 2
eg:
Table 1:
name age birth
bob 24 england
sally 26 scotland
jim 51 USA
Table 2:
name
bob
jim
So the SP would insert Sally into Table2. Anybody have any ideas?
INSERT INTO Table2(name)
SELECT DISTINCT Name
FROM table1 tab1
WHERE
NOT EXISTS (SELECT * FROM table2 tab2 WHERE tab1.name = tab2.name)
Got it!