I have a SQL user table structure like this (the identifier is randomly generated and not automatically 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 several deleted (deleted = 1) users with the same name, but only one changed user with the same name (therefore, adding Simon is fine, but Dave is not). How can I insert only if in one SQL query there is no record with the same name and deleted = 0? 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 the correct syntax.
source share