Delete rows with duplicated/repeated data in specified columns - mysql

I need To Delete Repeated Records (only if cod1 and Model are repeated) And Save only one of thats.
ID Cod1 Model
1 332 mdl1
2 332 mdl1
3 332 mdl2
4 450 mdl2
5 450 mdl2
The output must Be
ID Cod1 Model
1 332 mdl1
3 332 mdl2
4 450 mdl2
Really Thanks!

try this one. Replace occurrences of Table1 with your actual table name.
DELETE FROM Table1 WHERE ID IN
(SELECT * FROM
(SELECT T2.Id
FROM Table1 T1
INNER JOIN Table1 T2
ON T1.Cod1 = T2.Cod1 AND T1.Model = T2.Model
WHERE T2.ID > T1.ID
)T
);
sqlfiddle
In the inner most query, T1 will find matching T2 records when Cod1 and Model match and T2.Id will be the larger id. So it returns IDs of rows to be deleted. But Mysql doesn't allow you to delete from a query that is from the same table so we have to wrap that inside another SELECT statement to trick Mysql into letting you do it. And that's it.

Related

Select All from one table and fill values from another table on the result

I want get full column from one table and select sum from another table that have same id.
Eg:
table1 table2
id target id target achived
1 40 1 20
2 50 2 25
3 66
4 80
and i want to select all from table 1 and fill achived target results on it.
Eg:
id target target achived
1 40 20
2 50 25
3 66 0
4 80 0
how can i do that using mysql
Use this query:
select table1.*, case when table2.target_achieved is null
then 0
else table2.target_achieved
end as target_achieved
from table1 left join table2
on table1.id = table2.id
order by table1.id
Below query results in 0 instead of NULL
SELECT t1.id, target, COALESCE(target_achieved, 0) AS target_achieved
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.id;

Select records where ID is in the other table and status = 1

I need to get all the records where table1.user is in table2.userid and table2.country = 8 and table2.status = 1
This is the sample data in my database
table1
id user
-- ----
1 12
2 23
3 34
4 32
5 85
6 38
table2
id userid country_id status
-- ---- ----- ----
1 12 5 1
2 12 8 1
3 85 8 1
4 38 8 0
5 38 7 1
6 23 8 1
7 23 4 1
in this case I should only get the id #3 in table2
Inner join will do the job taking only record with match in the two tables.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 on t1.user = t2.userid
WHERE t2.country=8 and t2.status=1
You mean
"get all the records where table1.user is in table2.userid
with table2.country = 8 and table2.status = 1 ?"
If so, then:
Select * from table1 t1
Where Exists(Select * from table2
Where userId = t1.user
and country = 8
and status = 1)
Well to accomplish that is quite simple, you just need to make use of an inner join. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
In your case, the INNER JOIN will return all rows from table2 where the join condition is met.
For a more detailed explanation and examples just visit this link: http://www.w3schools.com/sql/sql_join.asp
based on your response to Adam's approach "
I tried this but it didn't work, yes it gets the records but the id #2 in table2 is also included. what I need to get is the id #3 in table2. Thanks :) ". I changed my SQL query to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON
table1.user = table2.userid
WHERE
table2.userid = 85
I assumed you only want the data of that particular userid in table2

Sql join, extract value from the second table and add it to the first one

I have 2 tables in Mysql. I need to join them somehow to get one value from second table into the first one.
TABLE 1
Day EmployeeId Total EmployeeName
1 2 20 Josh
1 1 20 Mike
2 2 5 Josh
2 1 10 Mike
3 3 5 Eric
TABLE 2
Day EmployeeId Max_Total
1 2 40
1 1 40
2 2 5
2 1 15
I need to get something like TABLE 3
Day EmployeeId Total EmployeeName Max_Total
1 2 20 Josh 40
1 1 20 Mike 40
2 2 5 Josh 5
2 1 10 Mike 15
3 3 5 Eric null
So this Max_Total column needs to be somehow created and populated.
This Day_EmployedId combination is unique in both tables and that should be used somehow to extract values from 2nd table and add it to the first one.
Sometimes first table can have more values, sometimes the second one, but the first one will always be the one that needs to be manipulated/added to.
Any hint will be appreciated. Thanks
You are looking for a left join on two fields:
select t1.*, t2.max_total
from table1 t1 left join
table2 t2
on t1.day = t2.day and t1.employeeid = t2.employeeid;
I would not recommend actually updating table1. You can generate the data as you need it. However, in order for an update to work, you need to add a column to the table first, and then update it.
You need to separate your tasks.
Alter your Table1 to add the column Max_Total
Write UPDATE query to update your Max_Total in your Table1.
Query:
UPDATE t1.Max_Total = t2.Max_Total
SET t1.
FROM Table1 t1
JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
If you are only concerned about getting a combined result set
SELECT t1.Day, t1.EmployeeId, t1.Total, t1.EmployeeName, t2.Max_Total
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
For more information on LEFT JOIN, you can study this tutorial.

select values missing from one table based upon values in another table

I am using mysql and have 2 tables
table1
id
324
325
328
350
420
650
850
950
table2
id mapping_id
324 1
325 2
328 3
350 4
420 5
650 1
850 2
I want to produce a list of all the DISTINCT field mapping_ids that are missing for the ids in table one. For example id 850 has a mapping_id of 2 so is missing 1,3,4,5 and id 950 is not even in table 2 and so is missing 1,2,3,4,5. This should give me a distinct list of 1,2,3,4,5.
I have tried various LEFT JOIN queries but cannot get the results I need. Thanks in advance.
You could build a matrix of id - mapping combinations using a cross join. A not in subquery can determine which parts of the matrix are empty:
select *
from table1 t1
cross join
(
select distinct mapping_id
from table2
) mappings
where not exists
(
select *
from table2 t2
where t2.id = t1.id
and t2.mapping_id = mappings.mapping_id
)
select t1.id, t2.mapping_id
from table1 t1, table2 t2
MINUS
select t2.id, t2.mapping_id
from table1 t1
inner join table2 t2 on t1.id = t2.id

How to delete records in one table based on the values in another table?

Here are two tables:
table1
cm_id cost
1 6.52
2 16.52
3 2.12
4 7.14
5 19.09
6 11.52
7 0.12
table2
um_id order_num name
1 517 tommy
2 518 bobby
3 519 scotty
4 520 faris
5 521 justine
6 522 sadie
7 523 nicole
cm_id and um_id represent the same thing so the cost can be tied to each order number, ie
SELECT table1.cm_id, table1.cost, table2.order_num, table2.order_num
FROM table1, table2
WHERE table1.cm_id=table2.um_id;
What is the single SQL statement I can use to delete rows from table1 where the order_num in table2 is between 518 and 520?
delete
from table1
where cm_id IN (select um_id from table2 where order_num between 518 and 520)
DELETE table1
FROM table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
AND (table2.order_num BETWEEN 518 AND 520)
--OR
DELETE
FROM table1
USING table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
WHERE (table2.order_num BETWEEN 518 AND 520)
EDIT:
There was a duplicate FROM and query has been changed as per Andriy M comments.
I prefer this way
delete from table1
using table1, table2
where table1.cm_id = table2.um_id
and table2.order_num >= 518
and table2.order_num <= 520;
use DELETE with subquery:
DELETE FROM table1 WHERE table1.cm_id IN (SELECT table2.um_id FROM table2 WHERE order_num>=518 and order_num<=520)
As IN has some performance limitations we can use delete command with simple join query like
delete x from table1 x,table2 y where x.cm_id=y.um_id;