我正在尝试创建一个查询,它将检查它是否存在于表中,如果它存在,它只会向数量qty
添加一个,否则它将从其他表中选择插入,在我的案例temp_sales
到目前为止我的sql。
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;
我一直在关注来自UPDATE if exists else INSERT in SQL Server 2008的stackoverflow回答,但我没有运气。
MySQL错误#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
我做错了什么?
修改
PL:vb.net DB:MySQL(xampp)
答案 0 :(得分:0)
感谢您的评论,我的不好。我正在关注SQL服务器查询,而不是专注于MySQL查询。
但是经过一些额外的搜索(当然是在评论员的帮助下),这里得到了答案 - 谢谢
答案:
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