简体   繁体   中英

How can I merge rows from two joined tables?

There are two tables, which can be joined, and the relationship is 1 to many . I wish the rows of results to be merged.

For example:

Table 1: contacts

.------------.----------.
| contact_id | username |
:------------+----------:
|          1 | user1    |
:------------+----------:
|          2 | user2    |
:------------+----------:
|          3 | user3    |
'------------'----------'

Table 2: documents

.-------------.------------.----------.
| document_id | contact_id | filename |
:-------------+------------+----------:
|           1 |          1 | abc.txt  |
:-------------+------------+----------:
|           2 |          1 | bcd.txt  |
:-------------+------------+----------:
|           3 |          1 | cde.txt  |
:-------------+------------+----------:
|           4 |          2 | 123,txt  |
:-------------+------------+----------:
|           5 |          2 | 234.txt  |
:-------------+------------+----------:
|           6 |          3 | xyz.txt  |
'-------------'------------'----------'

The result I wish I can get:

.------------.----------.---------------------------.
| contact_id | username |         filenames         |
:------------+----------+---------------------------:
|          1 | user1    | abc.txt, bcd.txt, cde.txt |
:------------+----------+---------------------------:
|          2 | user2    | 123.txt, 234.txt          |
:------------+----------+---------------------------:
|          3 | user3    | xyz.txt                   |
'------------'----------'---------------------------'

Updated:

SELECT c.contact_id, c.username, GROUP_CONCAT(d.filename) as filenames 
FROM contacts c 
LEFT JOIN documents d 
ON c.contact_id = d.contact_id 
GROUP BY c.contact_id

You should really post your attempts with your question, so that we can see what you have tried. In that way, it will be easy to push you in the right direction, as well as give the rest of us the impression that you have put some effort into the matter before asking the question. Stackoverflow is not a coding service.

To answer your question,

What you would like to do in this case, is to perform an INNER JOIN on your two tables, and have the MYSQL function, GROUP_CONCAT(); , in your SELECT statement.

When you look at your two tables, you have a coherent id (contact_id) that you should use in your INNER JOIN to link your two tables together.

You then, at the end, need to perform a GROUP BY to group your results accordingly, ie to group the results by contact_id .

Your SQL would look something like this:

SELECT
  tbl_contacts.contact_id,
  tbl_contacts.username,
  GROUP_CONCAT(tbl_documents.filename) as file_name
FROM
  tbl_contacts
INNER JOIN
  tbl_documents ON tbl_contacts.contact_id = tbl_documents.contact_id
GROUP BY
  tbl_contacts.contact_id

Working SQL fiddle

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