xuol001 发表于 2018-10-21 06:33:09

SQL语句之DWL、DCL语句

MariaDB > select * from tbl1;  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|1 | tom| NULL |       1 | M      |
  
|2 | tao| NULL |       1 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|4 | wang |   25 |       5 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.00 sec)
  

  
MariaDB > select * from tbl1 order by ClassID; # 按 ClassID 升序排序
  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|1 | tom| NULL |       1 | M      |
  
|2 | tao| NULL |       1 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|4 | wang |   25 |       5 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.01 sec)
  

  
MariaDB > select * from tbl1 order by ClassID,name; # 如果ClassID相同就按name升序排列
  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|2 | tao| NULL |       1 | M      |
  
|1 | tom| NULL |       1 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|4 | wang |   25 |       5 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.03 sec)
  

  
MariaDB > select * from tbl1 order by ClassID,name DESC;# 如果ClassID相同就按name降序排列
  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|1 | tom| NULL |       1 | M      |
  
|2 | tao| NULL |       1 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|4 | wang |   25 |       5 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.02 sec)
  

  
MariaDB > select * from tbl1 order by ClassIDDESC; # 按ClassID降序排列
  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|4 | wang |   25 |       5 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|1 | tom| NULL |       1 | M      |
  
|2 | tao| NULL |       1 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.00 sec)
  
                            # 按ClassID降序排列,如果相同就按name降序排列
  
MariaDB > select * from tbl1 order by ClassID DESC,name DESC;
  
+----+------+------+---------+--------+
  
| id | name | age| ClassID | gender |
  
+----+------+------+---------+--------+
  
|4 | wang |   25 |       5 | M      |
  
|3 | xiu| NULL |       2 | M      |
  
|2 | tao| NULL |       1 | M      |
  
|1 | tom| NULL |       1 | M      |
  
+----+------+------+---------+--------+
  
4 rows in set (0.00 sec)


页: [1]
查看完整版本: SQL语句之DWL、DCL语句