简体   繁体   中英

Can I concatenate certain fields of a table in a multitable SELECT in MYSQL?

I have a table with users, other table with user experiences and other with jobs (and much more). I want to SELECT all the users who applied for a job with a new field which is formed by the concatenation of all the experiences of the user, something like that:

SELECT users.*,jobs.id, jobs.name
FROM users
LEFT JOIN users.id = users_jobs.userId
LEFT JOIN jobs.id = users_jobs.jobId
LEFT JOIN users.id = user_experiences.userId

I need to add a field to the SELECT with all the experiences concatenated of each user. I am trying GROUP_CONCATENATE but it returns me only a row.

Any idea please?

Not completely sure I understood your question correctly, here is my attempt :

SELECT 
    users.*,
    jobs.id, 
    jobs.name,
    GROUP_CONCAT(user_experiences.the_field_you_want_to_concatenate) AS concatenated_field
FROM users
LEFT JOIN users.id = users_jobs.userId
LEFT JOIN jobs.id = users_jobs.jobId
LEFT JOIN users.id = user_experiences.userId
GROUP BY 
     users.*,
     jobs.id,
     jobs.name;

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