为什么这个 Sql 语句“order by”不显示?适当地
我有 sql:
SELECT t.TagText, COUNT(*) AS NumVidsWithTag
FROM tbl_Tag t
INNER JOIN tbl_Video_Tag_Xref x ON t.TagId=x.TagId
INNER JOIN tbl_Video v ON x.VideoId=v.VideoId
WHERE 6 IS NULL OR EXISTS (SELECT * FROM tbl_Video_Tag_Xref WHERE VideoId=6 AND TagId=t.TagId)
GROUP BY t.TagText
返回:
1 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
2 1
20 1
然后我将 sql 更改为
SELECT t.TagText, COUNT(*) AS NumVidsWithTag
FROM tbl_Tag t
INNER JOIN tbl_Video_Tag_Xref x ON t.TagId=x.TagId
INNER JOIN tbl_Video v ON x.VideoId=v.VideoId
WHERE 6 IS NULL OR EXISTS (SELECT * FROM tbl_Video_Tag_Xref WHERE VideoId=6 AND TagId=t.TagId)
GROUP BY t.TagText
ORDER BY t.TagText ASC
它仍然返回为:
1 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
2 1
20 1
我很困惑为什么这不能正确排序?
I have the sql:
SELECT t.TagText, COUNT(*) AS NumVidsWithTag
FROM tbl_Tag t
INNER JOIN tbl_Video_Tag_Xref x ON t.TagId=x.TagId
INNER JOIN tbl_Video v ON x.VideoId=v.VideoId
WHERE 6 IS NULL OR EXISTS (SELECT * FROM tbl_Video_Tag_Xref WHERE VideoId=6 AND TagId=t.TagId)
GROUP BY t.TagText
Which returns:
1 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
2 1
20 1
Then I changed the sql to
SELECT t.TagText, COUNT(*) AS NumVidsWithTag
FROM tbl_Tag t
INNER JOIN tbl_Video_Tag_Xref x ON t.TagId=x.TagId
INNER JOIN tbl_Video v ON x.VideoId=v.VideoId
WHERE 6 IS NULL OR EXISTS (SELECT * FROM tbl_Video_Tag_Xref WHERE VideoId=6 AND TagId=t.TagId)
GROUP BY t.TagText
ORDER BY t.TagText ASC
And it still comes back as:
1 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
2 1
20 1
Im confused as to why this wont sort properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它已正确排序。
TagText 是一个字符串,因此“11”“小于”“2”,例如
要按数字排序,请将 ORDER BY 更改为
It is sorted properly.
TagText is a string, so "11" is 'smaller than' "2", for instance
To sort it numerically, change your ORDER BY to
看起来 TagText 是一个 varchar 字段,而不是一个数字。 SQL 将根据文本排序规则对 varchar 字段进行排序 - Aa 在 B 之前,因此 19 在 2 之前。
Looks like TagText is a varchar field, rather than a number. SQL will order varchar fields based on the rules for sorting text - Aa comes before B, so 19 comes before 2.
TagText 是一个字符字段(nvarchar?),并且按字母数字顺序排序。
您需要将其转换为数字格式才能排序。
TagText is a character field (nvarchar?) and is being sorted alphanumerically.
You will need to convert it to numeric format in order to sort.
假设
tbl_Tag.TagText
列数据类型为varchar
或类似的类型。在这种情况下,这是正确排序的 - 按字母数字排序,19 在 2 之前; 100 将出现在 11 之前。
您需要将数据类型更改为数字类型。
您将能够在不更改表架构的情况下执行此操作:
本质上,只需更改 ORDER BY 子句即可将 TagText 列转换为
int
类型Assumedly
tbl_Tag.TagText
columns datatype isvarchar
or something similar.In which case, this is sorting correctly - sorting alphanumerically, 19 comes before 2; and 100 would come before 11.
You will need to change the datatype to a numeric type.
You will be able to do this without changing the schema of the table:
Essentially, just change the ORDER BY clause to convert the TagText column to type
int
这就是为什么因为列的数据类型不是
整数
。它是字符串,并且像字符串一样排序。因此
2
在19
之后可能的解决方案是:将列的数据类型更改为
integer
This is why because of the data type of column is not
integer
. it is string and sorted like string.hence
2
comes after19
Possible solution is that: change the data type of your column to
integer
我假设 t.TagText 是一个字符字段?将使用该字段的 ASCII 值对订单进行排序。上面的排序顺序是正确的。
I assume t.TagText is a character field? The order will sorted using the ASCII values of this field. The above sort order is correct.
我认为顺序错误,因为顺序列是文本(varchar/nvarchar)。尝试在 sql 的 select 部分转换为 int。
如果需要,请在此处投射/转换帮助
http://msdn.microsoft.com/en-us/library/ms187928.aspx
I believe it's ordering wrong because the order column is a text (varchar/nvarchar). Try converting to a int in the select section of the sql.
Cast/Convert help here if needed
http://msdn.microsoft.com/en-us/library/ms187928.aspx