cari

Rumah  >  Soal Jawab  >  teks badan

MySQL: Mengisih nombor pendua secara menaik

<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粉763748806P粉763748806496 hari yang lalu526

membalas semua(1)saya akan balas

  • P粉501683874

    P粉5016838742023-07-26 10:47:39

    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.

    balas
    0
  • Batalbalas