MySQL 查詢中別名 temp 為何返回 NULL?

MySQL 查詢中別名 temp 為何返回 NULL?

mysql 查詢中別名 temp 返回 NULL

問題:在 mysql 查詢中,使用以下語句查詢別名為 temp 的列時,卻返回了 null:

select str_to_date(plan_start_time, '%h:%i:%s') as temp,plan_start_time from base_stop_calendar

背景信息:

  • temp 是別名,并非截圖錯誤。
  • plan_start_time 是 varchar 類型。

解答:

在提供的 sql 語句中,格式化字符 “%h” 用于表示小時(12 小時制)。然而,plan_start_time 列中存儲的是 24 小時制的時間,因此使用 “%h” 會導致格式化失敗,從而返回 null。

解決方案:

要正確地格式化計劃開始時間,應該使用大寫字母 “h” 來表示 24 小時制的時間。以下是修改后的 sql 語句:

select str_to_date(plan_start_time, '%h:%i:%s') as temp,plan_start_time from base_stop_calendar

使用此語句后,別名 temp 將返回格式正確的日期和時間。

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享