gmyb gmyb - 1 year ago 59
SQL Question

sql delete with select conditional

maybe someone can help me.

I have 2 sql tables:

// groups
| id_group | namegroup |
| 30 | s |

// contacts
| name | group |
| juan | s |

I need to DELETE a group from ID, but no has contacts associated with it.
I test the following query but doesnt work.

FROM group
WHERE id_group = 30
count(*) AS id
FROM contacts co
INNER JOIN GROUP c ON co. GROUP = c.namegroup
WHERE c.id_group = 30
) = 0


Answer Source

If you define firigen key mysql will handle this issue and you do not need to do anything

In these cases(without forigen key) i usually first run following query:

select count(*) as id from contacts co inner join group c on where c.id_group=30

And say to user which if they can delete or not and if he could delete that row:

delete from group where id_group=30

I tried to find a solution like query which you had but could not and suggest this solution for you.