文章詳情頁
Mysql update 分組遞增 sql咨詢
瀏覽:78日期:2022-06-14 11:40:57
問題描述
表
idlevelsort_num110210320420520630730830930要求得到
idlevelsort_num111212321422523631732833934即頁面的展示順序如下
level1
1
2
level2
1
2
3
level3
1
2
3
4
目前的sql
set @index:=0; update t set sort_num = (@index:=@index+1) where level = 1 order by id;set @index:=0; update t set sort_num = (@index:=@index+1) where level = 2 order by id;set @index:=0; update t set sort_num = (@index:=@index+1) where level = 3 order by id;
不知有沒更簡(jiǎn)潔的sql可以實(shí)現(xiàn)上述的需求?
補(bǔ)充在線sql
問題解答
回答1:如果只是為了頁面展示,不需要更新表的數(shù)據(jù)吧。可以試試下面的sql
select level, @sort_num:=if(@prev_level=t1.level, @sort_num+1, 1)from (select * from t order by id) t1, (SELECT @sort_num:=1, @prev_level:=NULL) varorder by id
相關(guān)文章:
1. MySQL數(shù)據(jù)庫(kù)中文亂碼的原因2. dockerfile - 我用docker build的時(shí)候出現(xiàn)下邊問題 麻煩幫我看一下3. angular.js - 關(guān)于$apply()4. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???5. 如何解決Centos下Docker服務(wù)啟動(dòng)無響應(yīng),且輸入docker命令無響應(yīng)?6. css - C#與java開發(fā)Windows程序哪個(gè)好?7. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計(jì)表結(jié)構(gòu)的?8. nignx - docker內(nèi)nginx 80端口被占用9. dockerfile - [docker build image失敗- npm install]10. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。
排行榜

熱門標(biāo)簽