Answer the question
In order to leave comments, you need to log in
How to display one-to-many data in one row through SQL?
Hello everyone, I'm trying to design a database .. there are 2 tables:
companies
- id
- name
- employees
employees
- id
- name
- company_id
the company has many employees .. one-to-many relationship .. tell me through which operators you can make a sql query that could display a trace of the result .. ideally, an example of a query
like a join would not be suitable?
Mysql 5.6.17 is used
Answer the question
In order to leave comments, you need to log in
No, because there can be a million employees in one firm and 3 in another. The only thing you can do is to concatenate the employees with a comma or other group buy delimiter. If you describe the problem normally, then it will be possible to advise the most suitable crutch, or a normal solution to a real problem, and not a free fantasy on the topic.
UPD: Should be something like:
SELECT concat(c.name,',',GROUP_CONCAT(e.name)) cn
FROM `companies` c
LEFT JOIN `employees` e
ON c.id = e.company_id
GROUP BY c.name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question