MySQL MySQL多列asc顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9063135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL multiple column asc order
提问by user1135693
I am trying to run this query in ascending order:
我正在尝试按升序运行此查询:
SELECT title,project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY title, project_index ASC;
I need two columns in ascending order, but the above query returns results with only one column in ASC
order.
我需要按升序排列的两列,但上面的查询返回的结果只有一列ASC
。
回答by Lieven Keersmaekers
Ascending order is the defaultfor most (if not all)DBMS's so your statement is kind of weirdin that respect but nevertheless, you can specify an order for each individual column by adding the specifier ASC
or DESC
to it.
升序是默认的大多数(如果不是全部)的DBMS所以你的说法是一种怪异的在这方面但尽管如此,您可以通过添加指定单元指定每一列的顺序ASC
或DESC
给它。
Your statement then would become
你的陈述然后会变成
SELECT title
, project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY
title ASC
, project_index ASC
Edit
编辑
As been mentioned by @Arvo & @Dems, currently you are sorting firston title
and for identical titles on project_index
. If you want your project_index
sorted first, you have to place it first in the ORDER BY
clause.
正如@Arvo&@Dems被提及,目前正在排序第一的title
和在相同的标题project_index
。如果你想project_index
先排序,你必须先把它放在ORDER BY
子句中。
Your statement then becomes
你的陈述然后变成
SELECT title
, project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY
project_index ASC
, title ASC
and because ASC
is the default sort order, you can omit them alltogether
并且因为ASC
是默认排序顺序,所以您可以将它们全部省略
SELECT title
, project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY
project_index
, title
回答by Ivaylo Petrov
回答by IRSHAD
ORDER BY title ASC, project_index ASC;
ORDER BY 标题 ASC,project_index ASC;
SELECT title,project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY title ASC, project_index ASC;
AND you can add more columns like ORDER BY col1 ASC, col2 ASC, col3 DESC;
并且您可以添加更多列,例如 ORDER BY col1 ASC, col2 ASC, col3 DESC;
回答by php
You try to sort both columns in ascending order. In mysql, you can use multiple order in a query. But the preference for the order by is very important here. First one get the most preference and next one get second preference.
That means, Your query is
您尝试按升序对两列进行排序。在 mysql 中,您可以在查询中使用多个订单。但是对 order by 的偏好在这里非常重要。第一个获得最多的偏好,下一个获得第二个偏好。
这意味着,您的查询是
SELECT title,project_index FROM projectdetail
WHERE project_index BETWEEN 1 AND 6 ORDER BY title, project_index ASC;
Where, order by title got first preference. The mysql will order the 'title' column in ascending order at first and display the result. Then only it will order 'project_index' column. So you cann't get answer as you want.
其中,按标题排序优先。mysql 会首先按升序对 'title' 列进行排序并显示结果。然后只有它会订购“project_index”列。所以你不能得到你想要的答案。
回答by Saharsh Shah
Try this:
尝试这个:
SELECT title, project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY project_index, title;
回答by user3115056
You can try with the below and check--
您可以尝试使用以下方法并检查--
SELECT title,project_index
FROM projectdetail
WHERE project_index BETWEEN 1 AND 6
ORDER BY title, project_index
回答by Ashish v
As per your requirement/query i think it is impossible to do ordering more than 2 columns in same table. If you want to order based on value you can do like this.
根据您的要求/查询,我认为在同一个表中订购超过 2 列是不可能的。如果您想根据价值订购,您可以这样做。
SELECT lat,lon, title, zip, city, state, region,cantone
FROM company
WHERE title != '' AND state IN(1,3,4,5,6,7,9,2)
ORDER BY state=2,title asc
In above query it will first show all title in ascending order except state=2 and then shows all records with state=2 in last.
在上面的查询中,它将首先按升序显示除 state=2 之外的所有标题,然后最后显示 state=2 的所有记录。
回答by Aafaq Ahmad
Use
用
ORDER BY title ASC,project_index ASC
instead of
代替
ORDER BY title, project_index ASC;
give the order separately for both then it will work properly.
分别为两者下订单,然后它将正常工作。