MySQL更新查询条件

时间:2014-11-08 23:21:20

标签: mysql

是否可以将一个更新查询用作另一个(更新)查询中的条件?

如果是这样 - 它的语法是什么?

如果不是 - 您认为背后有原因吗?可能会阻止不良做法,或者只是不需要这个功能?

我没有问题,说 - 我只是想知道是否有可能。我在尝试用一个查询帮助别人的问题时遇到了这个问题(目前似乎更有趣)。

My queries on sqlfiddle.

我尝试了什么:(在示例中我只是在条件中的更新查询至少更改了一行时尝试将一个字段更新为主字段,但我最关心的是语法所以不要小心,如果那是“实际发生的事情”。

  1. 使用IF:

    UPDATE `data` SET `priority` = 
        IF (
            UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `priority` = 'PRIMARY' AND `status`='active'),
            'PRIMARY',`priority`) 
    WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active';
    
  2. 导致错误:

    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
     'UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `' at line 6:
    
    1. 使用CASE WHEN:

      UPDATE `data` SET `priority` = (CASE
          WHEN UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `priority` = 'PRIMARY' AND `status`='active') > 0 THEN 'PRIMARY'
          ELSE `priority`
          END)
      WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active';
      
    2. 导致错误:

      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
       'UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `' at line 2: 
      

      还有一些变种。

      万一你无法访问sqlfiddle - 我的查询:

      CREATE TABLE `data` (
       `id` int(13) NOT NULL AUTO_INCREMENT,
       `id_fk` int(13) NOT NULL,
       `priority` enum('PRIMARY'),
       `status` enum('active','deleted'),
        PRIMARY KEY (`id`),
       KEY `prime_address` (`id_fk`,`priority`)
      ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
      
      INSERT INTO `data` (`id_fk`,`priority`,`status`) values 
      ('1',NULL,'active'),
      ('1',NULL,'active'),
      ('1','PRIMARY','active'),
      ('1',NULL,'active'),('1',NULL,'active'),
      ('1',NULL,'deleted');
      
      #If query:
      #inner query test:
      #UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `priority` = 'PRIMARY' AND `status`='active');
      
      #outer query test:
      #UPDATE `data` SET `priority` = IF(true,'PRIMARY',`priority`) WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active'
      
      UPDATE `data` SET `priority` = 
      IF (
        UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `priority` = 'PRIMARY' AND `status`='active'),
        'PRIMARY',`priority`) 
      WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active';
      
      #CASE WHEN query test:
      
      UPDATE `data` SET `priority` = (CASE
          WHEN 1 > 0 THEN 'PRIMARY'
          ELSE `priority`
          END)
      WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active';
      
      #CASE WHEN query:
      UPDATE `data` SET `priority` = (CASE
          WHEN UPDATE `data` SET `status` ='deleted',`priority` = NULL WHERE (`id_fk` = 1 AND `priority` = 'PRIMARY' AND `status`='active') > 0 THEN 'PRIMARY'
          ELSE `priority`
          END)
      WHERE `id` = 1 AND `id_fk` = 1 AND `status` = 'active';
      

0 个答案:

没有答案