MYSQL将数据排序为模式

时间:2017-05-09 12:56:11

标签: mysql sorting

不确定这是否可行......

在MYSQL中给出这些数据:

 fnPresentLoading() {
     this.loading = this.loadingCtrl.create({
     content: 'Please wait...'
     });
   this.loading.present();

如何返回以这样的模式排序的结果集:

AAA
BBB
BBB
CCC
AAA
CCC
DDD
DDD

1 个答案:

答案 0 :(得分:2)

您可以使用变量:

SELECT mycol
FROM (
   SELECT mycol,
          @rnk := IF(@col = mycol, @rnk + 1,
                     IF(@col := mycol, 1, 1)) AS grp
   FROM mytable
   CROSS JOIN (SELECT @rnk := 0, @col := '') AS vars
   ORDER BY mycol) AS t
ORDER BY grp, mycol;

Demo here