insert if not exists mysql 5.5.24 - mysql

I have been looking around, however i can not see my error,
My query
INSERT INTO p_location_check (location_id) VALUES (1)
IF NOT EXISTS (SELECT approved, disapproved FROM p_location_check WHERE approved REGEXP '^1234568745$' OR disapproved = '^1234568745$' AND location_id=1);
after just for testing
INSERT INTO p_location_check (location_id) VALUES (1)
IF NOT EXISTS (SELECT approved, disapproved FROM p_location_check WHERE approved = 1234568745 OR disapproved = 1234568745 AND location_id=1);
reponse
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 'IF NOT EXISTS (SELECT approved, disapproved FROM
qp_location_check WHERE approve' at line 2
server version
Server version: 5.5.24-0ubuntu0.12.04.1

Edited:
Try this query -
INSERT INTO p_location_check (location_id) VALUES (1) FROM dual
WHERE (SELECT COUNT(*)
FROM p_location_check
WHERE approved = 1234568745 OR disapproved = 1234568745 AND location_id=1
) = 0;
Add your WHERE condition.

Related

MySQL 8 Transaction resulting in error for using multiple sql statements

I have 3 tables, one is named SKU_Data, and 2 are named Fabric_Code and Product_Type respectively.
SKU_Data has 2 foreign key columns, one stores id of Fabric_Code and the other stores id of Product_Type.
I wrote an SQL transaction to put data into SKU_Data. (Using MySQL 8)
START TRANSACTION;
SELECT id INTO #fabricId FROM Fabric_Codes WHERE Fabric_Code = 'SOME_CODE';
SELECT id INTO #productTypeId FROM Product_Types WHERE Product_Type = 'SOME_TYPE';
INSERT INTO SKU_Data (Item_Sku_Code, Date_Introduced, Fabric_Id, Product_Type_Id, CP)
VALUES ('SOME_STRING_ID', '2012-04-03 14:00:45', #fabricId, #productTypeId, 41);
IF (ERROR) THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
Now I am getting below mentioned error:
SQL Error [1064] [42000]: 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 'SELECT id INTO #fabricId FROM Fabric_Codes WHERE Fabric_Code = 'VELVETS';
SELEC' at line 3
Error position: line: 2
This error too vague to solve, any idea how to go about fixing this?
When I run the query SELECT id INTO #fabricId FROM Fabric_Codes WHERE Fabric_Code = 'VELVETS'; alone it works fine.
I tried changing the delimiter that also didn't work.
Do not use intermediate variables. Execute your action in single INSERT .. SELECT (which is a transaction itself):
INSERT INTO SKU_Data (Item_Sku_Code, Date_Introduced, Fabric_Id, Product_Type_Id, CP)
SELECT 'SOME_STRING_ID',
'2012-04-03 14:00:45',
Fabric_Codes.id,
Product_Types.id,
41
FROM Fabric_Codes
CROSS JOIN Product_Types
WHERE Fabric_Codes.Fabric_Code = 'SOME_CODE'
AND Product_Types.Product_Type = 'SOME_TYPE';

MARIADB : Insert and Update table based on data from another table

/* Here is the code i used to merge but i get error and am unable update
Error : SQL Error [1064][42000] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the righr syntax to use near 'Merge temp2 as t' at line 1
*/
BEGIN
MERGE temp2 as t
using temp1 as s ON (t.slno = s.slno)
-- Insert values when data no present
WHEN NOT MATCHED THEN INSERT VALUES
(s.slno,s.name,s.address);
-- Update when values present
WHEN MATCHED then UPDATE SET
t.slno = s.slno,
t.name = s.name,
t.address = s.address;
END
You could probably use this:
INSERT INTO temp2 (slno, name, address) SELECT slno, name, address FROM temp1
ON DUPLICATE KEY UPDATE
slno = VALUES(slno), name = VALUES(name), address = VALUES(address)

Creating a trigger which enters value in studies(rollno,code) table only if total credits of a student are less than 30

I am getting the error : #1064 - 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 '' at line 5.
CREATE TRIGGER db BEFORE INSERT ON studies
FOR EACH ROW
BEGIN
IF((select CAST(SUM(credits) as UNSIGNED) from ((SELECT credits from subject WHERE subject.code = NEW.code) union all (SELECT credits from subject,(SELECT code from studies where studies.rollno = NEW.rollno) as t1 WHERE subject.code = t1.code)) as t2) > 30) THEN
set NEW.rollno = NULL;
ENDIF;
END

select if and update issue

Am using this sql to do an update on duplicate key
IF (SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0 )
UPDATE mlm_settings SET value='2' WHERE user_id = 7
ELSE
BEGIN
INSERT INTO `mlm_settings` (`key`, `value`,`user_id`) VALUES ('notify_type', '2',7)
END
by i get an sql error in mysql saying
#1064 - 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 'IF (SELECT COUNT(*) FROM
`mlm_settings` WHERE `key` = 'notify_type' AND `user_id' at line 1
i can't figure what is the cause of the error, as the names of the tables are valid and the values are of the same data type
what could be the error?
You can only use IF statements in stored procedures, not normal queries.
If there's a unique index on (key, user_id) in the table, you can use:
INSERT INTO mlm_settings (`key`, value, user_id) VALUES ('notify_type', '2', 7)
ON DUPLICATE KEY UPDATE value = '2';
IF control block cannot be used OUTSIDE of functions. Try this:-
SELECT IF( EXISTS(
SELECT COUNT(*) FROM `mlm_settings` WHERE `key` = 'notify_type' AND `user_id`=7 >0), 1, 0)

Conditional update/insert in MySQL

I found here a topic about an MySQL IF, ELSE query,i adapted it but i can't figure it out what is the problem with it.
Here is the query:
IF (SELECT * FROM `jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THEN
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`) VALUES ('Imprimanta Laser',
'Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR');
ELSE UPDATE `jos_import03_07_2011` SET `pret` = '338.49' WHERE `cod_oem` = 'OP-4CL';
END IF;
And here is the error:
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 'IF (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') IS NULL THE' at line 1
This is the original post:
Conditional mySQL statement. If true UPDATE, if false INSERT
Thanks,
Sebastian
UPDATE
Error code for IF EXISTS:
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 'IF EXISTS (SELECT * FROM
`jos_import03_07_2011` WHERE `cod_oem` = 'OP-4CL') THEN' at line 1
Any reason you can't use the INSERT ... ON DUPLICATE KEY syntax?
INSERT INTO `jos_import03_07_2011` (`tip_imp`, `tip_produs`, `producator`,
`cod_intern`, `desc`, `cod_oem`, `pret`, `valuta`)
VALUES ('Imprimanta Laser', Piese Schimb', 'BROTHER', 'BR-200503', '', 'OP-4CL', '338.49', 'EUR')
ON DUPLICATE KEY UPDATE SET pret = VALUES(pret)
would be far more efficient: one less query and far less code to debug.