简体   繁体   中英

MySQL Case with Insert and Update

I need help writing a conditional query to copy data from one table to another, everything was pretty straight forward until I decided I wanted to include versioning in my application!

I have looked at some of the examples, but they are mostly oriented towards creating a mysql procedure, this is what I have tried:

        SELECT CASE 
            WHEN NOT EXISTS (
                SELECT `version`
                FROM `archive_courses`
                WHERE `original_course_id` = '$course_id'
                AND `version` = '$current_version'
            )
            THEN
                BEGIN
                    INSERT INTO `archive_course_users`      <------ syntax error
                    (`course_id`, `user_id`, `course_qty`)
                    SELECT @new_course_id, '$user_id', `course_qty`
                    FROM `current_course_users`
                    WHERE `course_id` = '$course_id'
                    AND `user_id` = '$user_id'
                END
            ELSE
                BEGIN
                    UPDATE `archive_course_users`
                    SET `course_qty` = (SELECT `course_qty` FROM `current_course_users` WHERE `user_id` = '$user_id')
                    WHERE `original_course_id` = '$course_id'
                    AND `user_id` = '$user_id'
                END
            END
        END

It seems the case is working /OK/ but throws a syntax error when it gets to my INSERT query. How should I be doing this?

It shouldn't work as well that way. Change your query to be like

               INSERT INTO `archive_course_users`     
                (`course_id`, `user_id`, `course_qty`)
                SELECT @new_course_id, '$user_id', `course_qty`
                FROM `current_course_users`
                WHERE `course_id` = '$course_id'
                AND `user_id` = '$user_id'
                AND NOT EXISTS (
            SELECT 1
            FROM `archive_courses`
            WHERE `original_course_id` = '$course_id'
            AND `version` = '$current_version');

have you tried "insert.. on duplicate key update" ?

Heres an example from the mysql manual:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

UPDATE table SET c=c+1 WHERE a=1;

http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

Hope it helps

As per @Rahul's answer I created two queries:

    //INSERT IF NOT EXISTS
        INSERT INTO `archive_course_users`
        (`course_id`, `user_id`, `course_qty`)
        SELECT @new_course_id, '$user_id', `course_qty`
        FROM `current_course_users`
        WHERE NOT EXISTS (
            SELECT `version`
            FROM `archive_courses`
            WHERE `original_course_id` = '$course_id'
            AND `version` = '$current_version'
        )
        AND `course_id` = '$course_id'
        AND `user_id` = '$user_id'

    //UPDATE IF EXISTS
        UPDATE `archive_course_users`
        SET `course_qty` = (SELECT `course_qty` FROM `current_course_users` WHERE `user_id` = '$user_id')
        WHERE EXISTS (
            SELECT `version`
            FROM `archive_courses`
            WHERE `original_course_id` = '$course_id'
            AND `version` = '$current_version'
        )
        AND `original_course_id` = '$course_id'
        AND `user_id` = '$user_id'

Cannot have statements inside a SELECT . So either

Move the code out of the SELECT :

 IF (...) THEN   -- Note:  No "CASE"; not "WHEN"
     INSERT ...
 ELSE
     UPDATE
 END IF

Or SELECT some kind of flag, then use an IF statement outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM