問(wèn)題:在 mysql 查詢中,使用以下語(yǔ)句查詢別名為 temp 的列時(shí),卻返回了 null:
select str_to_date(plan_start_time, '%h:%i:%s') as temp,plan_start_time from base_stop_calendar
登錄后復(fù)制
登錄后復(fù)制
背景信息:
- temp 是別名,并非截圖錯(cuò)誤。
- plan_start_time 是 varchar 類型。
解答:
在提供的 sql 語(yǔ)句中,格式化字符 “%h” 用于表示小時(shí)(12 小時(shí)制)。然而,plan_start_time 列中存儲(chǔ)的是 24 小時(shí)制的時(shí)間,因此使用 “%h” 會(huì)導(dǎo)致格式化失敗,從而返回 NULL。
解決方案:
要正確地格式化計(jì)劃開(kāi)始時(shí)間,應(yīng)該使用大寫字母 “h” 來(lái)表示 24 小時(shí)制的時(shí)間。以下是修改后的 sql 語(yǔ)句:
select str_to_date(plan_start_time, '%h:%i:%s') as temp,plan_start_time from base_stop_calendar
登錄后復(fù)制
登錄后復(fù)制
使用此語(yǔ)句后,別名 temp 將返回格式正確的日期和時(shí)間。