Mysql命令通过给出意想不到的结果

时间:2014-11-10 08:26:20

标签: mysql sql-order-by

当我运行这个mysql查询时,我得到了一个意想不到的结果顺序

SELECT 
    score.field_score_value AS field_score_value
FROM node n 
INNER JOIN field_data_field_game game ON n.nid = game.entity_id 
INNER JOIN field_data_field_score score ON n.nid = score.entity_id 
INNER JOIN field_data_field_student student ON n.nid = student.entity_id 
INNER JOIN field_data_field_first_name name ON student.field_student_target_id = name.entity_id 
INNER JOIN field_data_field_last_name last ON student.field_student_target_id = last.entity_id 
INNER JOIN student_level level ON student.field_student_target_id = level.uid 
WHERE (game.field_game_target_id = 910) 
AND (level.group_level = 'intermediate')
ORDER BY score.field_score_value ASC 

这是我的结果

10
11
14
4 

为什么ORDER BY得分.field_score_value ASC不起作用?

3 个答案:

答案 0 :(得分:1)

可能是因为score.field_score_value被定义为varchar。

答案 1 :(得分:1)

这是因为它是一个字符串值,而且这是一个自然的顺序。您可以强制将其视为数字

ORDER BY CAST(score.field_score_value AS UNSIGNED) ASC 

P.S:为什么首先得分为varchar

答案 2 :(得分:0)

可能是varchar类型。您可以使用:

 ORDER BY convert(score.field_score_value, decimal) ASC

ORDER BY CAST(score.field_score_value as SIGNED INTEGER) ASC

注意:但如果您将数据库从varchar更改为int / bigint会更好。