I need to write an SQL script that selects one record in table1, then does a lookup in the remaining tables in the database. If it doesn't find the record, I need delete the record from table1. Anyone provide some sample script?
One example
delete table1
where not exists (select 1
from Table2
where table1.SomeColumn = Table2.SomeColumn)
AND table1.SomeColumn = 5 --just an example,
Leave the AND out if you want to delete all the rows from table 1 that do not exist in table 2
you can also use LEFT JOIN or NOT IN
I have done things like this:
DELETE table1
FROM table1
WHERE table1.ID NOT IN (
SELECT RefID FROM Table2
UNION
SELECT RefID FROM Table3
...
)
Assuming RefID are FK's to table1.ID. Is this what you need?
DELETE FROM Table1 WHERE id=10 AND NOT EXISTS (SELECT * FROM Table2 WHERE id=10);
Very generally, (since you gave little details)
Delete Table1 t1
Where [Criteria to find table1 Record]
And Not Exists(Select * From Table2
Where pk = t1.Pk)
And Not Exists(Select * From Table3
Where pk = t1.Pk)
And Not Exists(Select * From Table4
Where pk = t1.Pk)
... etc. for all other tables
Related
I have two tables.
Table 1
Id
UpdateId
Name
Table 2
Table1ID
UpdateID
Address
Each time user update, system will insert record to table1. But for table2, system only insert record when there is update in address.
Sample data
Table 1
1,1,name1
1,2,name1
1,3,name1update
1,4,name1update
1,5,name1
1,6,name2
Table 2
1,1,address
1,4,addressupdate
I want to get the result as following
1,1,name1,address
1,2,name1,address
1,3,name1update,address
1,4,name1update,addressupdate
1,5,name1,addressupdate
1,6,name2,addressupdate
How to make use of join condition to achieve as above?
You can use a correlated subquery. Here is standard syntax, but it can be easily adapted to any database:
select t1.*,
(select t2.addressid
from table2 t2
where t2.table1id = t1.id and
t2.updateid <= t1.updateid
order by t2.updateid desc
fetch first 1 row only
) as addressid
from table1 t1;
you can use left join when you want to take all columns from left table t1 even though it doesn't match with the other table with column updateid on t2 table.
select t1.id,t1.updateid,t1.name,t2.address from table1 t1
left join table2 t2
on t2.updateid= t1.updateid
you can read more about joins here
I am trying to delete rows from one table. So this what i have done so far. I imported a .CSV file which created a temp table. I would like to delete the rows in my original table with matching with temp table.
I tried the following code :
Delete From Table1
Where postid and userid in (Select postid, userid
from Table2)
but it does not work.
The goal is to delete rows in Table 1 using Table 2.
A simple INNER JOIN should do the job:
DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid
This is just another funny way :
DELETE FROM Table1
WHERE STR(postid) + STR(userid)
IN (SELECT STR(postid) + STR(userid) FROM Table2)
As far as I understand you want to Delete from Table1 where you want to create a composite condition based on postid and userid the from Table2 the problem is that you subquery in(Select postid,userid from
Table2) is not returning the correct value as to match the in condition please modify the query to
DELETE T1
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.postid = T2.postid
AND T1.userid = T2.userid
as stated in the previous answer
You can use a Merge statement. In this case you're only interested in deletes. A semicolon is required after the delete section, followed by a final semicolon to end the merge statement. For example:
MERGE table1 AS target USING table2 AS source
ON target.postid = source.postid and target.userid = source.userid
WHEN Matched THEN DELETE;;
An example sqlfiddle: http://sqlfiddle.com/#!18/a932d/1/0
The Merge documentation: https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql
I want to delete an entry in a table where multiple fields match the results of another select subquery which takes data from another table.
query :
DELETE FROM table1
WHERE carriedby_circuit IN (SELECT
circuit_id
FROM table2
WHERE circuit_name IN (SELECT
t.sc_prod_service_id
FROM table3 t));
So I want to modify the query in such a way that if circuit_id form table2 is present in carriedBY_circuit or in CARRIED_CIRCUIT columns of table1. it should delete the row from table1.
You can try with merge:
merge into table1 t1
using (select t2.circuit_id from table2 t2 inner join table3 t3 on t2.circuit_name = t3.sc_prod_service_id) d
on (d.circuit_id = t1.carriedby_circuit or d.circuit_id = t1.carried_circuit)
when matched then delete;
Assuming you have 3 tables and you are working with ids. Table1, table2 and table3. Your best approach is to join table2 with table3. Then query its results, to delete from table1.
Example:
DELETE FROM table1
WHERE table1.id IN(SELECT distinct id FROM tabel1 UNION SELECT ID as id FROM tabel2);
PS: The union select will probably duplicate your Id´s, this is why I propose to use distinct.
I have table1 with columns:
def_id, def_name, username etc
I have another table table2 which has some association:
assoc_id,parent_id,child_id
The parent_id , child_id are actually def_id's from Table1 . They get inserted into Table2 based on parent_child relation user action in GUI.
Now I want to select all def_id for a particular username from Table1 and then use that input to delete all the records if those def_ids are part of the parent_id or child_id from Table2.
How do I do this in SQL? I am using Sybase database.
Any help will be appreciated
Delete Table2
Where parent_id In
(Select def_id from table1
Where username = #username) Or
child_id In
(Select def_id from table1
Where username = #username)
Or
Delete t2
From table2 t2
Where Exists
(Select * From Table1
Where def_id In
(t2.parent_Id, t2.child_Id))
An easy way is adding a subquery to the where clause.
DELETE ab
FROM AuthorArticle AS ab
WHERE ab.AuthID=(SELECT AuthID FROM Authors WHERE AuthorLastName='Yin')
Never used Sybase, but this is basic SQL and should work.
Try:
DELETE table2
FROM table2
INNER JOIN table1 ON table1.def_id IN (table2.parent_id, table2.child_id)
We have 3 tables (table1, table2, table3), and I need to delete all the rows from table1 that have the same ID in table2 OR table3. To see a list of all of these rows I have this code:
(
select
table2.ID,
table2.name_first,
table2.name_last,
table2.Collected
from
table2
inner join
table1
on
table1.ID = table2.ID
where
table2.Collected = 'Y'
)
union
(
select
table3.ID,
table3.name_first,
table3.name_last,
table3.Collected
from
table3
inner join
table1
on
table1.ID = table3.ID
where
table3.Collected = 'Y'
)
I get back about 200 rows. How do I delete them from table1? I don't have a way to test if my query will work, so I'm nervous about modifying something I found online and potentially deleting data (we do have backups, but I'd rather not test out their integrity).
TIA!
EDIT
You are correct, we are on MSSQL 2008. Thanks so much for all the replies, I will try it out tomorrow!
Try this:
DELETE FROM Table1 WHERE
ID IN
(
SELECT ID FROM Table2 WHERE Collected = 'Y'
UNION ALL
SELECT ID FROM Table3 WHERE Collected = 'Y'
)
To test this query you can create dupicate tables using into clause i.e.(I assume it is SQL Server):
SELECT * INTO DUP_Table1 FROM Table1;
SELECT * INTO DUP_Table2 FROM Table2;
SELECT * INTO DUP_Table3 FROM Table3;
and then run this query:
DELETE FROM DUP_Table1 WHERE
ID IN
(
SELECT ID FROM DUP_Table2 WHERE Collected = 'Y'
UNION ALL
SELECT ID FROM DUP_Table3 WHERE Collected = 'Y'
)
EDIT: Added the Collected Criteria and used UNION ALL (#Thomas: Thanks..) I think performance of UNION ALL is better than UNION when there is no need for uniqueness in the result.
DELETE FROM table1
WHERE EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id AND table2.collected = 'Y')
OR EXISTS (SELECT 1 FROM table3 WHERE table3.id = table1.id AND table3.collected = 'Y')
If you're feeling nervous about a big delete like this, put it into a transaction: that way you can at least check the row count before running commit (or rollback, of course :p)
Make sure foreign keys are setup properly and turn on cascade deletes. But if that's not an option, the correct SQL query is as follows:
begin tran
delete from table1
where exists(select * from table2 where table1.id = id and collected='Y')
or exists(select * from table3 where table1.id = id and collected='Y')
-- commit tran
-- rollback tran
if the SQL runs as expected, execute the "commit tran", otherwise execute the "rollback tran".