How can i construct a SQL query to delete how i want.
I have two tables.
Table 1.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
Table 2.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
The two tables are related by DateTime and UserID
Is there anyway i can create a query so that it deletes from table 2 if no rows in table1 have a matching DateTime & UserID.
Thanks
You can use LEFT JOIN :
DELETE table2
FROM table2 t2 LEFT JOIN table1 t1 ON t1.`DateTime` = t2.`DateTime`
AND t1.`UserID` = t2.`UserID`
WHERE t1.`UserID` IS NULL
DELETE
FROM table2 t2
WHERE NOT EXISTS
(
SELECT NULL
FROM table1 t1
WHERE (t1.userId, t1.dateTime) = (t2.userId, t2.dateTime)
)
First of all: create a backup before you delete lots of records :)
The idea:
DELETE FROM
table1
WHERE
NOT EXISTS (SELECT 1 FROM table2 WHERE table1.referenceColumn = table2.referenceColumn)
You can check which records will be deleted by replacing the DELETE with SELECT *
And now the solution
DELETE FROM
table2
WHERE
NOT EXISTS (
SELECT 1 FROM
table1
WHERE
table2.UserID = table1.UserID
AND table2.DateTime = table1.DateTime
)
Related
I created table_1 with a list of products and columns for price and quantity (both are currently NULL as a default for an empty value)
I have another table (table_2) with the same columns with info about price and quantity for each product.
How do I update table_1 with info from table_2?
I've tried to use subqueries with both UPDATE and INSERT INTO commands - Both didn't work.
I pasted the two tables here:
https://justpaste.it/8qusd
Consider the update/join syntax:
update table1 t1
inner join table2 t2 on t2.id = t1.id
set t1.price = t2.price, t1.quantity = t2.quantity
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".
I need to update a table consisting of million rows
there are two tables table1 and table2
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
and on this #resultset1 ID, I have to update table 1
UPDATE table1
SET x=true
where ID EXISTS IN (#resultset1)
there are millions of rows in both table. how do i do it?
And Can anyone say whats wrong with this, i am trying some alternative over join
UPDATE table1 t1
SET x=true
WHERE <condition> AND EXISTS(
SELECT* FROM (
SELECT *
FROM table2 t2
WHERE t2.field = t1.field
) AS result
WHERE<condition on resultset field>
);
Output the results of the first query to a file.
Convert the output to update statements, one update per id (using sed or whatever)
Split the statements into separate files, maybe a 1000 per file (using split or whatever)
Run each file as an sql script with a pause (eg 10 seconds) between each execution (to allow logs to update etc and spread the load), using a simple runner script the loops over the files
Why can't you simply do like below. Though I don't really see a need of group by and having provided if this is your actual query.
UPDATE table1
SET x=true
where ID IN (
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
)
Another good option would be JOIN both result set and do the UPDATE like
UPDATE table1 t1
JOIN
(
SELECT ID
FROM (
select ID from table1 where<condition>
) as result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
) X ON t1.ID = X.ID
SET t1.x=true
EDIT:
You can as well store the result of fist query in a temporary table (As already suggested by a_horse_with_no_name) and then do the UPDATE using a JOIN with the temporary table. Somethig like below
create temporary table idtemp(ID INT);
insert into idtemp
SELECT ID
FROM (
select ID from table1 where<condition>
) result1
INNER JOIN table2 ON result1.field=table2.field
GROUPBY table2.field
HAVING <condtion>
Finally do the update like
UPDATE table1 t1
JOIN idtemp it ON t1.ID = it.ID
SET t1.x=true
Im trying to copy row from table to another using 2 coluom only as the tow table schema is not identical ,
am getting this error
Operand should contain 1 column(s)
Any tips whats wrong with my statement ?
Insert table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Where Not Exists (
Select 1
From table1 As T2
Where
(T2.screenname = T1.screenname,T2.list_id = T1.list_id)
)
try to change where condition from (T2.screenname = T1.screenname,T2.list_id = T1.list_id) to (T2.screenname = T1.screenname AND T2.list_id = T1.list_id)
(note AND keyword instead of comma)
Did you try INSERT INTO...ON DUPLICATE KEY syntax?
See MySQL manual here
You can create a unique index in table1 on the columns screenname and list_id
Then use the following statement
Insert ignore into table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Also try this query -
INSERT INTO table1 (screenname, list_id)
SELECT screenname, list_id FROM table2 t2
LEFT JOIN table1 t1
ON t1.screenname = t2.screenname AND t1.list_id = t2.list_id
WHERE
t1.screenname IS NULL AND t1.list_id IS NULL;
Use simple INSERT IGNORE
INSERT table1 (screenname, list_id) SELECT screenname, list_id FROM table2