求助:按部门优先级排序并分组查询医生列表的MySQL查询语句
Solution to Sort by Department Priority then Doctor Priority in MySQL
Got it, let's break down how to get the exact formatted result you need. First, let's recap your tables and requirements to make sure we're aligned:
Your Tables
Doctor_department: Stores departments with their priority (a=1, b=2, c=3)Doctor: Stores doctors with their priority and assigned department (d1-d7 mapped to a/b/c as you listed)
Requirements
- Sort results first by department priority (ascending)
- Within each department, sort doctors by their own priority (ascending)
- Output the final format:
(a(d1,d2,d3)),(b(d4,d5)),(c(d6,d7))
MySQL Query to Get the Formatted Result
Here's the query that will do exactly what you need:
SELECT GROUP_CONCAT( CONCAT('(', dept.department_name, '(', dept.doctors, '))') ORDER BY dept.dept_priority ASC SEPARATOR ',' ) AS final_result FROM ( -- Subquery to group doctors per department, sorted by doctor priority SELECT dd.department_name, dd.priority AS dept_priority, GROUP_CONCAT(d.doctor_name ORDER BY d.priority ASC) AS doctors FROM Doctor d INNER JOIN Doctor_department dd ON d.department = dd.department_name GROUP BY dd.department_name, dd.priority ) AS dept;
How This Works
Let's break down the query step by step:
- Subquery: We first join the
DoctorandDoctor_departmenttables to link each doctor to their department's priority. UsingGROUP_CONCAT(d.doctor_name ORDER BY d.priority ASC)we combine all doctors in a department into a comma-separated string, sorted by their individual priority. We group by department name and department priority to ensure we get one row per department. - Outer Query: We take each department row from the subquery, wrap it in the required
(部门(医生列表))format usingCONCAT, then use anotherGROUP_CONCATto combine all these department blocks into a single string, sorted by department priority (ascending) with commas as separators.
Bonus: Laravel Eloquent Equivalent
If you want to implement this in Laravel instead of raw SQL, here's how you can do it:
// Get grouped doctors per department, sorted correctly $departmentGroups = DB::table('Doctor') ->join('Doctor_department', 'Doctor.department', '=', 'Doctor_department.department_name') ->select([ 'Doctor_department.department_name', DB::raw('GROUP_CONCAT(Doctor.doctor_name ORDER BY Doctor.priority ASC) as doctors'), 'Doctor_department.priority as dept_priority' ]) ->groupBy('Doctor_department.department_name', 'Doctor_department.priority') ->orderBy('dept_priority', 'asc') ->get(); // Format the result to match your required output $finalResult = $departmentGroups->map(function($group) { return "({$group->department_name}({$group->doctors}))"; })->implode(',');
The issue with your original Doctor::orderBy('priority', 'ASC')->get(); is that it only sorts by the doctor's own priority and doesn't account for the department's priority at all—we need to join the department table to access that critical sorting key.
内容的提问来源于stack exchange,提问作者Arafat Rahman
相关产品推荐
相关产品推荐

