I have 3 tables on my SQLite db as follow:
TABLE first_names
id
name
gender
TABLE last_names
id
name
TABLE names
id
first
last
gender
birthday (null)
used (default 0)
Where first and last are unique and also have a index with both being unique on the names table.
In order to fed names table I have used the below query, which worked the first time but after I had added a new name to the last_names and tried to use it again it failed.
INSERT INTO names (first, last, gender)
SELECT f.name AS first, l.name AS last, f.gender AS gender
FROM first_names f
LEFT JOIN last_names l
WHERE f.name NOT IN (SELECT first FROM names) AND
l.name NOT IN (SELECT last FROM names)
Have also tried using (first, last, gender, [birthday], [used]) with values as null AS [birthday], 0 AS [used] but since it birthday is set to null on the table design and used to 0 as default and it worked on the first time didnt think it would be needed nor it made any difference anyway.
I would think it is because of the unique fields however I do have a WHERE to make sure the names are not already in the table names.
I have tried the query using C# and SQLiteAdmin both executing without requesting result or else it doesnt work.
What is wrong with my query and why it only works once ?
Try using:
WHERE NOT EXISTS ( SELECT * FROM names n WHERE n.first=f.name AND n.last=l.name)