U
U
usdbuy2022-03-31 21:08:44
SQL
usdbuy, 2022-03-31 21:08:44

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
L6VF4i7.png

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

2 answer(s)
T
ThunderCat, 2022-03-31
@ThunderCat

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

B
Beliyadm, 2022-03-31
@beliyadm

No way, no normalization, it was necessary to always make three tables, add a relationship between companies and employees between companies (consider further - the same database works in accounting)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question