I have a ‘users’ SQL table structure like this (the ID is randomly generated, not auto-incremented):
ID name deleted lastActive
3242 Joe 0 20-6-2012 23:14
2234 Dave 0 20-6-2012 23:13
2342 Simon 1 20-6-2012 23:02
9432 Joe 1 20-6-2012 22:58
There can be multiple deleted (deleted=1) users with the same name, but only 1 undeleted user with the same name (so adding Simon is fine, but Dave is not). How can I insert only if there is not a record already with the same name and deleted=0, in one SQL query? I need something like this:
INSERT INTO users (ID, name) VALUES ($id, $name)
WHERE NOT EXISTS (SELECT 1 FROM users WHERE name = $name AND deleted = 0)
But this is not correct syntax.
Setup a LEFT JOIN with
This is the query