mysql max as_mysql使用max函数+将类似123的字符型数据转换成数据类型
一 獲取每天用戶數(shù)最多的時段
從最多一詞可以看出,我們?nèi)缫コ脩魯?shù)最多的時段,需要使用max()函數(shù)了,
數(shù)據(jù)表result中個字段為:stat_day,usernum,stat_hour,部分數(shù)據(jù)如下所示
stat_day usernum stat_hour
20181210 190 2018121000
20181210 112 2018121001
20181210 71 2018121002
......
20181211 118 2018121101
......
20181212 1110 2018121022
...
20181213 448 2018121023
...
20181214 210 2018121100
...
取值sql如下:
select stat_day,usernum ,stat_hour
from result t1
where usernum = (select max(usernum) from result as t2
where t1.stat_day = t2.stat_day)
發(fā)現(xiàn)結(jié)果如數(shù)據(jù)不符,究其原因是userum數(shù)據(jù)導入數(shù)據(jù)庫的時候有數(shù)據(jù)庫自動創(chuàng)建的字段,為字符類型,若要按照數(shù)據(jù)類型進行排序則需要首先將數(shù)據(jù)庫的usernum字段修改為數(shù)值類型
數(shù)據(jù)類型轉(zhuǎn)換(字符型-數(shù)值類型)
轉(zhuǎn)換方式有以下三種
方法一:SELECT CAST('12345' AS SIGNED);
方法二:SELECT CONVERT('12345',SIGNED);
方法三:SELECT '12345'+0;
修改后的查詢sql如下:
select stat_day,CAST(usernum AS SIGNED),stat_hour
from result t1
where CAST(usernum AS SIGNED) = (select max(CAST(usernum AS SIGNED)) from result as t2
where t1.stat_day = t2.stat_day)
此時結(jié)果已經(jīng)正常輸出
總結(jié)
以上是生活随笔為你收集整理的mysql max as_mysql使用max函数+将类似123的字符型数据转换成数据类型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python时间格式_python 格式
- 下一篇: SQLyog客户端使用教程