I am trying to create a query, that will check if it exist in a table, if it exist, it will just add one to quantity qty else it will insert from a selecting from other table, in my case temp_sales
here's my sql so far.
SET
TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; IF EXISTS (
SELECT
pid
FROM
temp_sales
WHERE
barcode = '4800556410652'
) BEGIN
UPDATE
temp_sales
SET
qty = qty + 1
WHERE
barcode = '4800556410652' END ELSE INSERT INTO temp_sales (
0,
(
SELECT
products.ID,
products.product_sprice as price,
1,
1 * price,
'4800556410652',
'101',
'admin'
WHERE
barcode = '4800556410652'
)
) END COMMIT TRANSACTION;
I have been following this stackoverflow answer from UPDATE if exists else INSERT in SQL Server 2008 but I have no luck.
MySQL error #104 #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 'TRANSACTION' at line 1
what did I do wrong?
EDIT
PL: vb.net
DB: MySQL(xampp)
Thanks for the comments, my bad. I was following a SQL server query instead of focusing on MySQL query.
But here's the answer after a few additional searches (of course with the help of the commentators) - thanks
Answer:
INSERT into temp_sales (
ID, pid, price, qty, total, barcode,
pos_id, teller
)
SELECT
'0',
ID as pid,
products.product_sprice,
1,
products.product_sprice,
#barcode,
'101',
'admin'
FROM
products
WHERE
barcode = #barcode ON DUPLICATE KEY
UPDATE
qty = qty + 1,
total = price * qty
Related
I've got a question about SQL (php), I want to INSERT data in my table. But I want to use the IF NOT EXIST value.
What I've tried:
INSERT INTO vrienden (id, userid, vriendmetid, accepted) VALUES (null, '1', '20', '0') WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')
I'm not sure what's wrong, because I get the following 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 'WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')' at line 1
Thanks.
You want insert . . . select, not insert . . . values:
INSERT INTO vrienden (id, userid, vriendmetid, accepted)
SELECT x.*
FROM (select null as id, '1' as userid, '20' as vriendmetid, '0' as accepted) x
WHERE NOT EXISTS (SELECT 1 FROM vrienden v WHERE v.userid = x.userid AND v.vriendmetid = x.vriendmetid);
However, you probably shouldn't be doing this in the INSERT. Instead, create a unique index/constraint:
create unique index unq_vrienden_userid_vriendmetid on vrienden(userid, vriendmetid);
This way, the database will ensure uniqueness of the columns, so your application does not have to.
IF NOT EXISTS (SELECT 1 FROM Products WHERE name='Iphone1' AND manufacturer='appl') THEN
INSERT INTO Products(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');
ERROR: syntax error at or near "IF" LINE 1: IF NOT EXISTS (SELECT 1
FROM Products WHERE name='Iphone1' A...
Can anyone help me understand what I am doing wrong?
MySQL only supports the IF statement in programming blocks -- stored procedures, functions, and triggers. Hence you cannot do what you want that way.
Instead, you can just write a single query:
INSERT INTO Products (product_id, name, category, manufacturer)
SELECT product_id, name, category, manufacturer
FROM (SELECT 10000 as product_id, 'IphoneZ' as name, null as category, 'appl' as manufacturer) t
WHERE NOT EXISTS (SELECT 1 FROM Products p WHERE p.name = t.name and p.manufacturer = t.manufacturer);
Actually, though, it is best to have the database directly enforce this sort of uniqueness. You can do so with a unique constraint/index:
CREATE UNIQUE INDEX unq_product_manufacturer_name ON product(manufacturer, name);
Then you can write an query to ignore errors by doing:
INSERT INTO Products (product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl')
ON DUPLICATE KEY UPDATE category = VALUES(category);
The ON DUPLICATE KEY doesn't do anything -- it just serves to avoid returning an error if a duplicate value is inserted.
You are missing END IF keyword at the end of IF statement. And also, this SQL statement can be only used in a Routine block such as Stored Procedure or Stored Function.
Your SQL should be like this:
IF NOT EXISTS (SELECT 1 FROM Products WHERE name='Iphone1' AND manufacturer = 'appl') THEN
INSERT INTO Products(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');
END IF;
Is it MySQL or SQL server?
I think you type wrongly : name='Iphone1' . It should be 'IphoneZ'.
You used single quotes in some places and double quotes in some places.
If it is mysql then try below query
INSERT INTO Products(product_id, name, category, manufacturer)
SELECT * FROM (select 10000,'IphoneZ', null, 'appl') AS tmp_table
WHERE NOT EXISTS (SELECT 1 FROM Products_arun WHERE name='IphoneZ'
AND manufacturer='appl') LIMIT 1;
If it is SQL server then try below query
IF NOT EXISTS (SELECT 1 FROM product WHERE name='IphoneZ' AND manufacturer='appl')
INSERT INTO product(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');
This is my query :
SELECT vehicle,
CASE
WHEN vehicle IS NOT NULL
THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values
(SELECT `vehicle` FROM `tbl_missions` WHERE `id` = 4 ), (SELECT `id` FROM `tbl_users` WHERE `id` = 12))
FROM tbl_missions WHERE id = 4;
I need to insert a row to tbl_vehicle_on_user when vehicle is not null on id 4.
When i execute this query i receive this error from mysql workbench,
01:24:49 SELECT vehicle, CASE WHEN vehicle IS NOT NULL THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM tbl_missions WHERE id = 4 ), (SELECT id FROM tbl_users WHERE id = 12)) FROM tbl_missions WHERE id = 4 Error Code: 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 'INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM `tbl_mi' at line 4 0.000 sec
And i get a red line under 'INTO' when i hover over it, it says 'Syntax error, unexpected INTO, expecting ('.
I don't know what it means i tried to search the web but couldn't find anything if you know how to fix this i will appreciate it if you answer my question :)
THANKS!!
If you already know the userid should be 12, then just use 12 instead of SELECT id FROM tbl_users WHERE id = 12. Here is a valid insert-select statement.
INSERT INTO tbl_vehicle_on_user (vehicle, userid)
SELECT `vehicle`, 12 userid
FROM `tbl_missions`
WHERE `id` = 4 and vehicle is not null;
So--i'm having an issue with my code. I'm testing it directly in the console and getting an "syntax 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 EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6'
AND uid ='1') T' at line 1
IF EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6' AND uid ='1')
THEN UPDATE user_inventory SET resource_count = resource_count+1 WHERE resource_id = 6 AND uid = 1
ELSE INSERT INTO user_inventory(uid, resource_id, resource_count) VALUES (1, 6, 1);
I've never used the IF EXISTS clause before... So I'm not sure what i've done wrong.
The SQL IF statement can only be used within a stored program. You can't use it within a generic SQL context as you have attempted.
However, you do not need to use IF here:
Define a suitable uniqueness constraint on your user_inventory table:
ALTER TABLE user_inventory ADD UNIQUE (uid, resource_id)
Use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO user_inventory
(uid, resource_id, resource_count)
VALUES
(1, 6, 1)
ON DUPLICATE KEY UPDATE
resource_count = resource_count + 1
I need to be able to add a set of values into a table every time a new user is added into my system.
When the user is added, I want to look into my 'Tags' table, and insert a new entry into the 'Tag_Score' table for each of the IDs in the 'Tags' table.
I tried the following based on something i found online but although the logic seems sound, it doesn't seem to be working
DECLARE #LoopVar INTEGER
SET #LoopVar = ( SELECT MIN(Tag_Score.T_ID)
FROM Tags ) WHILE #LoopVar IS NOT NULL
BEGIN
INSERT INTO `a3360218_DD`.`Tag_Score` (
`A_ID` ,
`T_ID` ,
`Score` ,
`Visits`
)
VALUES (
'" . $accountID . "', #LoopVar , '0', '0'
)
SET #LoopVar = ( SELECT MIN(Tag_Score.T_ID)
FROM TheTable
WHERE #LoopVar < T_ID )
END
The error given is:
#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 'WHILE #LoopVar IS NOT NULL
BEGIN
INSERT INTO `a3360218_DD`.`Tag_Score` (
' at line 2
Thanks in advance
Matt
You haven't posted any table layouts, so I'm shooting in the dark here, but this kind of query should work for you:
INSERT INTO Tag_Score (A_ID, T_ID, Score, Visits)
SELECT A_ID, T_ID, 0, 0
FROM Tags
WHERE A_ID = $accountID;
Note the removal of all those unnecessary back-ticks (ie `) that are only necessary when you have a schema name that is a reserved word, and look how readable it is now!
Keep things simple - avoid complex SQL if you can.
I think you might want CROSS JOIN, or maybe just a regular insert from...
insert into Tag_Score
select accountID, t.ID, 0, 0
from tags t
-- optional order by
-- order by t.ID