dongle7637 2018-09-05 16:21
浏览 27
已采纳

进行更新时订购表

I have the following table where order by priority ASC

 ----------------------
|priority |activity   |
|---------|-----------| 
|   1     |act1       |
|   2     |act2       |
|   3     |act3       |
|   4     |act4       |
|   5     |act5       |
|---------|-----------|

JSON where I make an update.

Add Method but it does not work as I wish

  <?php
      //update.php
      include_once('../include/conexion.php');

        $query = "
               UPDATE ACT_schedule SET ".$_POST["name"]." = '".$_POST["value"]."'
               WHERE id_schedule = '".$_POST["pk"]."'";
            $result=mysqli_query($conn, $query);

            if ($result) {
            $query2 = "UPDATE ACT_Agenda SET prioridad = CASE 
                   WHEN prioridad >= " . $_POST['value'] . "
                   THEN prioridad + 1 ELSE prioridad  END
                   WHERE id_agenda <> '" . $_POST['pk'] . "'";
            mysqli_query($conn, $query2);
            echo "YES";
        }  ?>

What I want to do is order the priority, if I update the act5 that has priority 5 to priority 1, the priority changes and that means that the priority of the act1 must change to 2 and so on until the act4 change to priority 5.

It works well if I update the last priority. But if I update the act4 to priority 1 the ones below should not be updated but they do it by adding +1 (act5 priority 5 is 6).

Something like that I would like if I update act4 to priority 1

   ----------------------
    |priority |activity   |
    |---------|-----------|
    |   1     |act4       |
    |   2     |act1       |
    |   3     |act2       |
    |   4     |act3       |
    |   5     |act5       |
    |---------|-----------|

I hope I explained well. Greetings.

  • 写回答

4条回答 默认 最新

  • douchun1900 2018-09-08 04:51
    关注

    From your code it's not 100% clear which are the appropriate $_POST variables to use in the update query so here is a pure MySQL solution. First create the demo table:

    CREATE TABLE agenda (`priority` int, `activity` varchar(4)); 
    INSERT INTO agenda (`priority`, `activity`)
      VALUES (1, 'act1'), (2, 'act2'), (3, 'act3'), (4, 'act4'), (5, 'act5');
    SELECT * FROM agenda ORDER BY priority;
    

    Output:

    priority    activity
    1           act1
    2           act2
    3           act3
    4           act4
    5           act5
    

    To update, use the following query. I have used variables @n for the new priority and @a for the activity to modify; in your PHP code you would remove the SET statements below and replace @n and @p in the update query with the appropriate $_POST values. In this example I am shifting act4 to priority 2:

    SET @a = 'act4';
    SET @n = 2;
    UPDATE agenda SET priority = CASE WHEN priority BETWEEN @n AND (SELECT * FROM (SELECT priority FROM agenda WHERE activity=@a) a) AND activity != @a THEN priority + 1 
                                      WHEN activity = @a THEN @n
                                      ELSE priority
                                 END;
    

    Now we can look at the modified table:

    SELECT * FROM agenda ORDER BY priority;
    

    Output:

    priority    activity
    1           act1
    2           act4
    3           act2
    4           act3
    5           act5
    

    SQLFiddle Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?