I'm having trouble with the following query:
INSERT INTO `pasajeros` ( `nombre`,`apellidos`,`email`,`password`,`telefono`)
VALUES ('Alexandra','Gonzalez','[email protected]','1234567!','04242344556' )
WHERE NOT EXISTS ( SELECT * FROM `transportistas` WHERE `email` = '[email protected]');
What do I have wrong in the code? I need to do an Insert only if mail is not used in that other table. I just added the statement WHERE NOT EXISTS
to the side but it seems I don't meet the required syntax, how can I improve my code?
In order to use the
where not exists
, you need aselect
. So you need to convert the clausevalues
to aselect
:Another way, following the same principle, a bit more complicated, but which prevents you from repeating the value for the email in 2 places, is to use a derived table:
Another idea could be with a condition like this:
You can also do it with
JOIN
:A temporary table is created with the value of the email and it is compared with the email column of carriers, if it IS NULL, the insertion is made because said email does not exist in carriers.
Dear, I did my research work and I have found a way in which you could achieve your goal, I hope it works for you. Cheers