Trying to check if a name exists (in another table) and add to insert query in one step. I’ve discovered if the submitted empNum isn’t in the User database, the query errors with: Column ‘lastName’ cannot be null. Can an if statement be used in the subquery something like below?
INSERT INTO Password_Reset (empNum, lastName, fromIP, dateReset)
VALUES (34567,
IF(SELECT COUNT(*) FROM User WHERE empNum = 34567, SELECT lastName FROM User WHERE empNum = 34567, '---'),
'192.168.1.1',
NOW())
Your subqueries should be in parenthesis :
There many other ways you can do the same as already mentioned by others, but your version will also work after adding required “()”.