MySQL: Mengisih nombor pendua secara menaik
P粉763748806
P粉763748806 2023-07-25 12:41:47
0
1
521
<p>我有一个表格,看起来像这样:</p> <table class="s-table"> <kepala> <tr> <th>id</th> <th>nilai</th> </tr> </kepala> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>4</td> <td>2</td> </tr> <tr> <td>5</td> <td>2</td> </tr> <tr> <td>6</td> <td>2</td> </tr> </tbody> </table> <p>我想要在某种循环中按照这种方式对它们进行递增排序。</p> <table class="s-table"> <kepala> <tr> <th>id</th> <th>nilai</th> </tr> </kepala> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>4</td> <td>2</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>5</td> <td>2</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>6</td> <td>2</td> </tr> </tbody> </table> <p>我相信这可以很容易地用PHP完成,但是我想看看是否可以使用SQL来实王<p>
P粉763748806
P粉763748806

membalas semua(1)
P粉501683874

Gunakan fungsi tetingkap ROW_NUMBER() dalam klausa ORDER BY:

SELECT *
FROM tablename
ORDER BY ROW_NUMBER() OVER (PARTITION BY `values` ORDER BY id), 
         `values`;

Anda boleh lihat ini demo.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan