简体   繁体   中英

How do I return a single row as well as multple entries using group_concat in MySQL

I hope this one is a relatively easy one to solve, but here is the problem I'm faced with:

SQL Query:

SELECT
    job.job_id
    ,job_reference
    ,job_title
    ,jc.name as job_category_name
    ,ps.name as position_status_name
    ,group_concat(pt.name ORDER BY pt.position_type_id SEPARATOR ' / ') as position_type_name
    ,cty.name as city_name
    ,min.amount as min_salary
    ,max.amount as max_salary
    ,job_description
    ,skills_required
    ,additional_notes
FROM job
INNER JOIN job_category jc ON job.job_category_id = jc.job_category_id
INNER JOIN position_status ps ON job.position_status_id = ps.position_status_id
INNER JOIN job_position_type jpt ON job.job_id = jpt.job_id
INNER JOIN position_type pt ON jpt.position_type_id = pt.position_type_id
INNER JOIN city cty ON job.city_id = cty.city_id
INNER JOIN salary min ON job.min_salary_id = min.salary_id
INNER JOIN salary max ON job.max_salary_id = max.salary_id;

The query is only returning the entries where job_position_type table has multiple entries and not if job_position_type only has a single entry.

Cheers,

Tim

OK so I managed to sort out my problem pretty easily actually, here is my original SQL as specified above:

SELECT
    job.job_id
    ,job_reference
    ,job_title
    ,jc.name as job_category_name
    ,ps.name as position_status_name
    ,group_concat(pt.name ORDER BY pt.position_type_id SEPARATOR ' / ') as position_type_name
    ,cty.name as city_name
    ,min.amount as min_salary
    ,max.amount as max_salary
    ,job_description
    ,skills_required
    ,additional_notes
FROM job
INNER JOIN job_category jc ON job.job_category_id = jc.job_category_id
INNER JOIN position_status ps ON job.position_status_id = ps.position_status_id
INNER JOIN job_position_type jpt ON job.job_id = jpt.job_id
INNER JOIN position_type pt ON jpt.position_type_id = pt.position_type_id
INNER JOIN city cty ON job.city_id = cty.city_id
INNER JOIN salary min ON job.min_salary_id = min.salary_id
INNER JOIN salary max ON job.max_salary_id = max.salary_id;

And this is what the query returns:

显示所有串联行的位置类型名称

And the solution was just to add a group by clause so that it seperates the rows, here is my update SQL:

SELECT
    job.job_id
    ,job_reference
    ,job_title
    ,jc.name as job_category_name
    ,ps.name as position_status_name
    ,group_concat(pt.name ORDER BY pt.position_type_id SEPARATOR ' / ') as position_type_name
    ,cty.name as city_name
    ,min.amount as min_salary
    ,max.amount as max_salary
    ,job_description
    ,skills_required
    ,additional_notes
FROM job
INNER JOIN job_category jc ON job.job_category_id = jc.job_category_id
INNER JOIN position_status ps ON job.position_status_id = ps.position_status_id
INNER JOIN job_position_type jpt ON job.job_id = jpt.job_id
INNER JOIN position_type pt ON jpt.position_type_id = pt.position_type_id
INNER JOIN city cty ON job.city_id = cty.city_id
INNER JOIN salary min ON job.min_salary_id = min.salary_id
INNER JOIN salary max ON job.max_salary_id = max.salary_id
GROUP BY pt.position_type_id;

And here is the expected result I wanted:

职位类型ID按职位类型ID分组

Thanks once again to @Adrien Brunelat for taking a look at my question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM