R
R
Roman Rakzin2015-07-30 18:07:29
SQL
Roman Rakzin, 2015-07-30 18:07:29

Sql DELETE with nested SELECT?

Good afternoon.
There are tables
Semesters(Id,Name,CreatedBy)
Groups(Id,Name,SemesterId) //SemesterId is bound to Semesters.Id
We need to delete data from the Groups table, let's say the user sends - delete group 1, then we need to check (via join) whether he is the creator of the "Semester" in which this group is located and if yes, then delete it.
Why exactly? It’s just that if a “too smart dude” comes across, so that he doesn’t send the IDs of those groups that do not belong to him for deletion

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Rakzin, 2015-07-30
@TwoRS

delete from groups WHERE SemesterId IN (SELECT id FROM semesters WHERE userid=7) AND groups.id=4
Check for user 7 who wants to delete group 4

S
Sanan Yuzb, 2015-07-30
@Sanan07

delete G from Groups
inner join Semesters S on G.SemesterId=S.id
where exists
(// check that he is the creator)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question