tags:

views:

20

answers:

1

What is wrong with this statement? It works with sqlite and MS sql (last time i tested it)

I would like a select unless there is no match with name or email. Then i would like to insert. I have it as one statement because its easy to keep concurrent safe as one statement. (its not that complex of a statement).

INSERT INTO `user_data` (
`last_login_ip`, `login_date`,
...) SELECT 
@0,
@14 WHERE not exists (SELECT * FROM `user_data` WHERE `name` = @15 OR 
`unconfirmed_email` = @16 LIMIT 1);

Exception

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE not exists (SELECT * FROM `user_data` WHERE `name` = 'thename' OR 
`unconfirmed' at line 31
+3  A: 

You have no FROM clause in your SELECT. If you look carefully at the documentation for SELECT you can see that although FROM is optional, if there is a WHERE clause then the FROM is required:

SELECT ...
[FROM table_references
[WHERE ...]
...]

If you don't need a table you can use FROM DUAL.

Mark Byers