0

I have the following sql query running and it is giving me error 1064, syntax error.

IF NOT EXISTS (select * from locations where STREET_ADDRESS = 'test') 
BEGIN
    insert into locations (STREET_ADDRESS) values ('test') 
end;

Can someone please help me out? It seems so simple yet it will not run. Thanks.

Also, I'm running MySQL version 5.6.11

Aaron
  • 39
  • 4

3 Answers3

0

Also you can try this

INSERT INTO locations (STREET_ADDRESS)
SELECT 'test' FROM DUAL
WHERE NOT EXISTS (
   SELECT * from locations where STREET_ADDRESS = 'test'
) LIMIT 1;
Sky
  • 3,330
  • 1
  • 12
  • 12
0

Try this

IF ((select * from locations where STREET_ADDRESS = 'test')=0 )
BEGIN
insert into locations (STREET_ADDRESS) values ('test') 
end;
pareto
  • 186
  • 5
0

you should use this type of sample code as:

   INSERT INTO table_listnames (name, address, tele)
    SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
    WHERE NOT EXISTS (
        SELECT name FROM table_listnames WHERE name = 'Rupert'
    ) LIMIT 1;

click here

Community
  • 1
  • 1
jmail
  • 5,308
  • 3
  • 16
  • 32