我需要从DB Week的月份和日期中检索数据 能够从MYSQL查询中获取数据
SELECT cnt, `creationdate`, week, weekname,
DATE_ADD(firstOfMonth,INTERVAL (week-1) WEEK) as 'Week Start',
IF(DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY) > eom,
eom, DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY)) as 'Week End'
FROM (
SELECT COUNT(`firstname`) AS 'cnt', `creationdate`,
FLOOR((DAYOFMONTH(`creationdate`) - 1) / 7 +1) AS week,
CONCAT('Week ',FLOOR((DAYOFMONTH(`creationdate`) - 1) / 7) +1) AS weekname, DATE_ADD(`creationdate`,interval -DAY(`creationdate`)+1 DAY) AS firstOfMonth,
LAST_DAY(`creationdate`) as 'eom'
FROM `UserDetails` WHERE DATE_FORMAT(`creationdate`,'%m/%Y')='06/2017' GROUP BY week ) a
原来是将am转换为laravel Raw Query
$monthOfWeaks = DB::table("UserDetails")
->select(DB::raw('cnt, creationdate, week, weekname,
DATE_ADD(firstOfMonth,INTERVAL (week-1) WEEK) as WeekStart,
DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY) > eom, eom, DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY)) as WeekEnd'))
->select(DB::raw('COUNT(`firstname`) as cnt, creationdate,
FLOOR((DAYOFMONTH(creationdate) - 1) / 7 +1) as week,
CONCAT(Week,FLOOR((DAYOFMONTH(creationdate) - 1) / 7) +1) as weekname,
DATE_ADD(creationdate,interval -DAY(creationdate)+1 DAY) as firstOfMonth,
LAST_DAY(creationdate) as eom'))
->whereRaw("DATE_FORMAT(creationdate,'%m/%Y')='06/2017')")
->groupBy(DB::raw("WEEK(creationdate)"))
->get();
似乎有些错误,但我无法在laravel中修复 请提供一些意见以解决我的问题? 谢谢。
答案 0 :(得分:1)
您可以使用
DB::select("
SELECT cnt, `creationdate`, week, weekname,
DATE_ADD(firstOfMonth,INTERVAL (week-1) WEEK) as 'Week Start',
IF(DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY) > eom,
eom, DATE_ADD(firstOfMonth,INTERVAL ((week-1)*7+6) DAY)) as 'Week End'
FROM (
SELECT COUNT(`firstname`) AS 'cnt', `creationdate`,
FLOOR((DAYOFMONTH(`creationdate`) - 1) / 7 +1) AS week,
CONCAT('Week ',FLOOR((DAYOFMONTH(`creationdate`) - 1) / 7) +1) AS weekname, DATE_ADD(`creationdate`,interval -DAY(`creationdate`)+1 DAY) AS firstOfMonth,
LAST_DAY(`creationdate`) as 'eom'
FROM `UserDetails` WHERE DATE_FORMAT(`creationdate`,'%m/%Y')='06/2017' GROUP BY week ) a
");
如果您没有要添加到来自请求/其他第三方的查询中的任何参数(因此没有SQL注入风险)