zoukankan      html  css  js  c++  java
  • MySQL拖拽排序

    -- ----------------------------
    -- Table structure for TestSort
    -- ----------------------------
    DROP TABLE IF EXISTS `TestSort`;
    CREATE TABLE `TestSort`  (
      `Id` int(0) NOT NULL AUTO_INCREMENT,
      `Name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
      `OrderNum` int(0) NOT NULL,
      PRIMARY KEY (`Id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of TestSort
    -- ----------------------------
    INSERT INTO `TestSort` VALUES (1, '名称1', 1);
    INSERT INTO `TestSort` VALUES (2, '名称2', 2);
    INSERT INTO `TestSort` VALUES (3, '名称3', 3);
    INSERT INTO `TestSort` VALUES (4, '名称4', 4);
    INSERT INTO `TestSort` VALUES (5, '名称5', 5);
    INSERT INTO `TestSort` VALUES (6, '名称6', 6);
    INSERT INTO `TestSort` VALUES (7, '名称7', 7);
    INSERT INTO `TestSort` VALUES (8, '名称8', 8);
    INSERT INTO `TestSort` VALUES (9, '名称9', 9);
    INSERT INTO `TestSort` VALUES (10, '名称10', 10);
    INSERT INTO `TestSort` VALUES (11, '名称11', 11);
    INSERT INTO `TestSort` VALUES (12, '名称12', 12);
    INSERT INTO `TestSort` VALUES (13, '名称13', 13);

    实现拖拽排序

    -- 从下往上拖
    -- 把第10个拖到第1位,范围[1-9],逻辑+=1
    
    
     UPDATE
             TestSort t1,
             TestSort t2
            SET t1.OrderNum = t1.OrderNum + 1,
             t2.OrderNum = 1
            WHERE
            t2.OrderNum = 10
     
     AND t1.OrderNum BETWEEN 1
                    AND 10-1;
    
    
    select * from TestSort order by Ordernum asc;
    
    -- 从上往下拖拽
    -- 把第1个拖到第10个,范围[1-9],逻辑-=1
    
     UPDATE
             TestSort t1,
             TestSort t2
            SET t1.OrderNum = t1.OrderNum +(-1),
             t2.OrderNum =10
            WHERE
            t2.OrderNum = 1
     AND t1.OrderNum BETWEEN 1
                    AND 10;
    
    
    
    
    select * from TestSort order by Ordernum asc;

    原文链接:https://blog.csdn.net/Mr_LiYyang/article/details/103226949

    作者: jamesbing
    提示: 欢迎转载,但是必须保留本文的署名 jamesbing (包含链接)
  • 相关阅读:
    linux最常用命令记录(一)
    2020centos解决“nginx 403 Forbidden"错误的故事
    nginx显示静态html爆502 bad gateway的错误提示
    codeigniter框架的使用感受和注意事项
    网闸
    抗DDOS防火墙
    负载均衡
    上网行为管理
    漏洞扫描系统
    网络分析系统
  • 原文地址:https://www.cnblogs.com/gaobing/p/14679829.html
Copyright © 2011-2022 走看看