多个升序在Mysql中不起作用

时间:2017-03-09 17:33:46

标签: mysql sorting

我有一个名为request的表,有三列称为Id, want, teach。我必须按升序显示wantteach列,并且我不想显示NULL和空行。

我尝试仅查询want列按升序显示,但teach列未按升序显示。

  SELECT want, teach FROM request WHERE want IS NOT NULL OR want !='' AND teach IS NOT NULL OR teach !='' GROUP BY want, teach ORDER BY want ASC, teach ASC

如果我写ORDER BY教ASC,想要ASC然后教学列以升序显示并想要显示DESC命令。 你能帮帮我吗?

Chech左侧栏是想要和右侧栏教。我得到这样的输出。左侧列是ASC顺序,但右侧显示DESC enter image description here

2 个答案:

答案 0 :(得分:1)

首先,您需要了解订单

如果你给第1,第2个asc命令,它会在第1列中出现并依赖于它将asc第2列。 例如

1st  2nd               
1    b
2    d
3    a
2    a
1    a

按转换顺序

 1st   2nd 
  1     a
  1     b
  2     a
  2     d
  3     a

所以你需要不同的查询

     SELECT want FROM request WHERE want IS NOT NULL OR want !='' GROUP BY 
 want ORDER BY want ASC

     SELECT teach FROM request WHERE teach IS NOT NULL OR teach !='' GROUP
 BY  teach ORDER BY teach ASC

答案 1 :(得分:0)

你在这里没有得到多重命令。

首先,默认情况下,大多数dbms排序为ASC

多次排序取决于排序列的顺序,这意味着 - 或者说 - : -

“嘿sql!从我的表中选择一些结果,按col1排序并按照这个顺序对它们进行分组,然后按照这个排序或分组的数据集对它们进行排序。”

这可以说是多维排序。

以此为例:

+------+---------+-------+                   +------+---------+-------+
|  id  |   want  | teach |                   |  id  |   want  | teach |
+------+---------+-------+                   +------+---------+-------+
|   1  |    1    |   2   |    ORDER BY       |   2  |    1    |   1   |
|   2  |    1    |   1   |       want asc,   |   1  |    1    |   2   |
|   3  |    2    |   1   |       teach asc   |   4  |    1    |   2   |
|   4  |    1    |   2   |                   |   3  |    2    |   1   |
+------+---------+-------+                   +------+---------+-------+

当您按照以下方式恢复已排序列的顺序时:

+------+---------+-------+                   +------+---------+-------+
|  id  |   want  | teach |                   |  id  |   want  | teach |
+------+---------+-------+                   +------+---------+-------+
|   1  |    1    |   2   |    ORDER BY       |   2  |    1    |   1   |
|   2  |    1    |   1   |       teach asc,  |   3  |    2    |   1   |
|   3  |    2    |   1   |       want asc    |   1  |    1    |   2   |
|   4  |    1    |   2   |                   |   4  |    1    |   2   |
+------+---------+-------+                   +------+---------+-------+