0

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)

Community
  • 1
  • 1
Nimitz E.
  • 95
  • 1
  • 13

1 Answers1

0

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    
Nimitz E.
  • 95
  • 1
  • 13