0

I'm using MariaDB 10.4.18, I'm trying to check if a certain value in a specific column already exists; example of the table:

Name Money Weapons Skin
First first_value first_value first_value
Second second_value second_value second_value

I would like to check if the value "First" in the column "Name" already exists, if true do nothing else creates the values.

I'm using as documentation this

IF NOT (SELECT * FROM players WHERE Name = '${player.name}')
THEN
INSERT INTO players (Name, Money, Weapons, Skin) VALUES ('${player.name}',100,'weapon_pistol,weapon_pistol_mk2', 'player_zero')
END IF;

The error is:

(node:10596) UnhandledPromiseRejectionWarning: Error: (conn=212, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF'

Thank you for your help.

Fitz
  • 17
  • 6
  • May you share the JavaScript code too? – evolutionxbox Jun 07 '21 at 11:49
  • First of all, you should not create queries with string templates but use parameterized queries. Second, this code may lead to racecoditions, and thus probably to inconsistencies in your data. If you want your `name` column to be unique, you should create a `UNIQUE` constraint and handle errors originating from that contraint appropriately ... – derpirscher Jun 07 '21 at 11:54
  • try this INSERT INTO players (Name, Money, Weapons, Skin) SELECT * FROM (SELECT 'yourName', 'yourMony', 'yourSkin') AS tmp WHERE NOT EXISTS ( SELECT name FROM players WHERE name = 'yourName' ) LIMIT 1; – Hamada Jun 07 '21 at 12:01

0 Answers0