hiredate DESC的订单在mysql中未按预期工作
我在mysql表中有一列hiredate DESC的订单在mysql中未按预期工作,mysql,sql,Mysql,Sql,我在mysql表中有一列hiredate。我必须为该列选择varchar数据类型,因为有时我得到的hiredate值为'xxxx xx xx'或'zzzz-zz-zz',我需要将这些值保存在数据库中 我的问题是,当我使用 select distinct agentname from agentdata where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz') order by hiredate desc select distinct
hiredate
。我必须为该列选择varchar数据类型,因为有时我得到的hiredate
值为'xxxx xx xx'或'zzzz-zz-zz',我需要将这些值保存在数据库中
我的问题是,当我使用
select distinct
agentname
from agentdata
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz')
order by hiredate desc
select distinct
agentname,
hiredate
from agentdata
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz')
order by hiredate desc
子句,它没有返回正确排序的数据。但是当我用
select distinct
agentname
from agentdata
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz')
order by hiredate desc
select distinct
agentname,
hiredate
from agentdata
where hiredate not in ('xxxx-xx-xx','zzzz-zz-zz')
order by hiredate desc
子句,它返回正确的排序数据
区别仅在于select语句中的
hiredate
字段。我不知道Mysql这种行为的原因。您需要按日期强制转换并按日期排序
ORDER BY str_to_date(hiredate, '%Y-%m-%d')
您向我们显示的两个查询是不同的,并且不会返回相同的列。在我看来,比较它们有点像苹果和橙子。是的,这两个是不同的,所以如果要正确排序结果集,在select子句中必须有列名吗?如果我只想要按hiredate排序的agentname呢?str_to_date(hiredate,“%Y-%m-%d”)对我不起作用。