具有动态值的MySQL列

时间:2015-11-20 23:08:57

标签: mysql sql

我有3张桌子

  1. tbl_jobs as jobs jobs.job_id = job_applicants.job_id
  2. tbl_jobs_applicants为 job_applicants
  3. tbl_applicants为申请人 job_applicants.applicant_id = applicants.applicant_id
  4. 我想在视图中显示作业中申请人的姓名(用逗号(fname lname)连接

    SELECT 
    jobs.jobs_id,
    {Mark Cruz, Steve Blake, Stephen Raynold}
    

    这是我的表结构: Table Structure

1 个答案:

答案 0 :(得分:1)

USE group_concat

<强> SQL Fiddle Demo

SELECT job_id, GROUP_CONCAT(fullname ORDER BY fullname ASC SEPARATOR ', ') AS applicants
FROM ( 
    SELECT j.job_id, concat(f_name, ' ', l_name) fullname
    FROM jobs  j
    join job_applicants ja
       on j.job_id = ja.job_id
    join applicants a
       on ja.applicant_id = a.applicant_id
) as j
GROUP BY job_id

<强>输出

| job_id |             applicants |
|--------|------------------------|
|      1 | emily swan, jhon twain |
|      3 |             jhon twain |
|      4 |             emily swan |