dongpan1416 2019-04-22 21:50
浏览 28

如何使用左连接和分组依据和列总和为2个表编写Laravel Eloquent查询

I have a user table and daily_attendance table. In users table i have id column as primary key, name column where i store employee name and In salary table i have emplyeeid column where i store user id, todaysdate columns where i store the date, month column and year column

I want to get the full month salary of each employee with employee name in single row.

This is my code

$query = DB::table('users')->select(\DB::raw('users.id, users.name,SUM(daily_attendance.day_salary) as day_salary'))
                                   ->leftJoin('daily_attendance', 'daily_attendance.employee_id', '=', 'users.id')
                                   ->where('daily_attendance.month',$current_month)
                                   ->where('daily_attendance.year',$year)
                                   ->where('daily_attendance.status',1)
                                   ->groupBy('users.id')
                                   ->get();

its giving me employee id and salary of the employee.But i am not getting the employee name.If i use users.* it gives me access violation error. please help me to do thi.

can i achieve this with group_concat or using laravel sub queries..

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画