带插入和更新的MySQL案例

时间:2015-12-01 14:58:32

标签: php mysql sql syntax innodb

我需要帮助编写一个条件查询来将数据从一个表复制到另一个表,一切都非常直接,直到我决定在我的应用程序中包含版本控制!

我已经看了一些例子,但是他们主要是为了创建一个mysql程序,这就是我试过的:

        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

似乎情况正常/确定/但是当它到达我的INSERT查询时会抛出语法错误。 我该怎么做?

4 个答案:

答案 0 :(得分:1)

它不应该那样工作。将您的查询更改为

               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');

答案 1 :(得分:0)

你试过"insert.. on duplicate key update"吗?

以下是mysql手册中的一个例子:

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

希望有所帮助

答案 2 :(得分:0)

根据@ Rahul的回答,我创建了两个查询:

    //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'

答案 3 :(得分:0)

SELECT内无法包含语句。所以要么

将代码移出SELECT

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

SELECT某种标志,然后在外面使用IF语句。