操作MySQL數據進行記錄的隨意排序


說到排序,想必大家都知道MySQL中的“ORDER BY”這個關鍵詞吧,使用它可以實現查詢數據根據某一字段(或多個字段)的值排序,那么如何實現數據的任意排序操作呢?

其實這里我所說的“隨意排序”,本質上來說是一種假象,后台SQL語句中依然使用到了ORDER BY關鍵詞,只不過我在數據表中加入了一個字段標記序號,前台中所謂的“排序”操作實際是對記錄的排序號進行交換操作而已,如下圖所示:

例如有如下數據顯示:

js實現部分代碼:

 1     function moveTop(id) {//上移
 2         $.ajax({
 3             url: '',//請求接口
 4             type: 'POST',
 5             data: {id: id},//需要上移的記錄主鍵
 6             dataType: 'json',
 7             success: function (data) {//成功返回data
 8                 if (data.success) {
 9                     showMsg("上移成功");
10                     setTimeout(function () {
11                         query();
12                     }, 1000);
13                 } else if (data.message) {
14                     showNote(data.message);
15                 } else {
16                     showNote("上移失敗");
17                 }
18             },
19             error: function () {
20                 showNote("上移失敗");
21             }
22         });
23     }
24 
25     function moveDown(id) {//下移
26         $.ajax({
27             url: '',
28             type: 'POST',
29             data: {id: id},
30             dataType: 'json',
31             success: function (data) {
32                 if (data.success) {
33                     showMsg("下移成功");
34                     setTimeout(function () {
35                         query();
36                     }, 1000);
37                 } else if (data.message) {
38                     showNote(data.message);
39                 } else {
40                     showNote("下移失敗");
41                 }
42             },
43             error: function () {
44                 showNote("下移失敗");
45             }
46         });
47     }
48 
49     function moveByShowSort(id) {//根據指定的顯示序號進行移動 注意:這里的序號和MySQL中記錄的序號沒有關系,只是頁面上顯示的序號 50         var $showSort = $("#showSort" + id);//獲取想要移動到的記錄顯示序號
51         var showSort = Number($showSort.val().trim());
52         var total = '${pageInfo.total}';//獲取記錄總數
53         if (showSort.length === 0) {
54             showNote("請輸入序號");
55             return;
56         } else {
57             if (isNaN(showSort)) {
58                 showNote("請輸入數字");
59                 return;
60             }
61             if (showSort <= 0 || showSort > total) {
62                 showNote("請輸入1~" + total + "之間的序號");
63                 return;
64             }
65         }
66         $.ajax({
67             url: '',
68             type: 'POST',
69             data: {id: id, showSort: showSort},
70             dataType: 'json',
71             success: function (data) {
72                 if (data.success) {
73                     showMsg("移動成功");
74                     setTimeout(function () {
75                         query();
76                     }, 1000);
77                 } else if (data.message) {
78                     showNote(data.message);
79                 } else {
80                     showNote("移動失敗");
81                 }
82             },
83             error: function () {
84                 showNote("移動失敗");
85             }
86         });
87     }

后台則根據獲取到的id查詢對應需要交換的記錄id,再根據id查詢sort排序號,最后二者交換即可。

SQL語句如下(使用mybatis編寫):

查詢被上移交換的記錄id:
<select id="getTopBannerIdById" resultType="string">
        SELECT id
        FROM b_banner
        WHERE sort &lt; (SELECT sort FROM `b_banner` WHERE id = #{id})
        ORDER BY sort DESC
        LIMIT 1
    </select>

查詢被下移交換的記錄id:
<select id="getDownBannerIdById" resultType="string">
        SELECT id
        FROM b_banner
        WHERE sort &gt; (SELECT sort FROM `b_banner` WHERE id = #{id})
        ORDER BY sort
        LIMIT 1
    </select>

查詢被交換的記錄id,攜帶輸入的顯示序號參數
<select id="getBannerIdByShowSort" resultType="java.lang.String">
        SELECT id
        FROM b_banner
        ORDER BY sort
        LIMIT #{showSort}, 1
    </select>

改變排序號就是根據id和sort進行記錄的更新操作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM