在我的表中,我将强制列作为字符串(例如:“ 20170502 ”)。
我想从该字符串中获取星期几,因此我尝试将其转换为日期格式,但无法正常工作。
$event = VEventMp::where('event_seq', '=', $mypage_data->event_seq)
->select(
'ENFORCE_PLACE',
'ENFORCE_FROM',
\DB::raw("DATE_FORMAT(STR_TO_DATE(ENFORCE_FROM,'%y/%m/%d'), '%m月%d日')
AS day"),
\DB::raw('(CASE WHEN WEEKDAY(day) = 0 THEN "月"
WHEN WEEKDAY(start_time) = 1 THEN "火"
WHEN WEEKDAY(start_time) = 2 THEN "水"
WHEN WEEKDAY(start_time) = 3 THEN "木"
WHEN WEEKDAY(start_time) = 4 THEN "金"
WHEN WEEKDAY(start_time) = 5 THEN "土"
WHEN WEEKDAY(start_time) = 6 THEN "日" END) AS weekday')
->first();
我在哪里弄错了?
谢谢!
答案 0 :(得分:0)
您遇到了两个问题,首先,如果日期格式类似于20170502
,那么您必须将其与STR_TO_DATE
进行匹配,因此将其更改为:
STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')
第二,您不能在查询的SELECT
部分使用别名,因此您需要将CASE
表达式更改为
(CASE WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 0 THEN "月"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 1 THEN "火"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 2 THEN "水"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 3 THEN "木"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 4 THEN "金"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 5 THEN "土"
WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 6 THEN "日" END) AS weekday