Update table with incremented value grouped by column
ต้องการให้สร้างลำดับของ sorder ของข้อมูลในกลุ่มที่มีค่า tpid เดียวกัน โดยเริ่มนับ sorder จาก 1 เป็นต้นไป และเริ่มนับ 1 ใหม่เมื่อเปลี่ยนค่า tpid
SET @id := null, @no := null; UPDATE `jobtr` SET sorder = IF(tpid = @id, @no := @no+1, @no := 1), tpid = (@id := tpid) ORDER BY tpid, sorder, jobtrid;
หากไม่ต้องเริ่มต้น group
SET @no = 0; UPDATEusers
SET uid = @no := @no+1 ORDER BYuid
ASC;
ฟิลด์คุมมีมากกว่า 1 ฟิลด์
SET @org := null, @psn := null, @no := null; UPDATEtest
SETcollectionNo
=NULL WHEREformName
= "DRUG"; UPDATEtest
SETcollectionNo
= IF(orgId
= @org &&psnId
= @psn, @no := @no+1, @no := 1) ,orgId
= (@org :=orgId
) ,psnId
= (@psn :=psnId
) WHEREformName
= "DRUG" ORDER BYorgId
,psnId
,autoId
;
Relate topics
- MySql backup
- การเก็บ JSON ใน MySQL ด้วย PHP
- ย้ายบ้านให้ MySql
- UPDATE multiple tables in MySQL using LEFT JOIN
- Column value swap
- my.cnf
- Make Ubuntu to be a web server (LAMP)
- Geometric - เก็บพิกัดกันยังไง เรื่องที่ผมยังไม่รู้
- Reset password root ใน mysql
- Sql injection
- MySQL / PHPMyAdmin - Export Table to Server
- How to install Lighttpd+PHP4+MySql5 on Ubuntu 8.10
- Change my life to Ubuntu