You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:按部门优先级排序并分组查询医生列表的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

  1. Sort results first by department priority (ascending)
  2. Within each department, sort doctors by their own priority (ascending)
  3. 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:

  1. Subquery: We first join the Doctor and Doctor_department tables to link each doctor to their department's priority. Using GROUP_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.
  2. Outer Query: We take each department row from the subquery, wrap it in the required (部门(医生列表)) format using CONCAT, then use another GROUP_CONCAT to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:49:37