I have 2 tables: manuscript and manuscript_log.
I would like to display for each manuscript the corresponding logs on the same line.
Example: I have two manuscripts 1 and 2. The manuscript 1 has 2 logs and the manuscript 2 has 3 logs.
I would like to get two results in the query, grouped by the manuscrit id:
manuscript_id manuscript_log
1. 1,2
2. 3,4,5
SELECT manuscript.id, manuscript_log.log_number
FROM manuscript INNER JOIN manuscript_log
ON manuscript.id = manuscript_log.manuscript_id
In SQL, you can also concatenate numerical data from the table in the same way as we concatenate strings. The CONCAT function can also be used to join numeric values.
The difference here is while CONCAT is used to combine values across columns, GROUP_CONCAT gives you the capability to combine values across rows. It's also important to note that both GROUP_CONCAT and CONCAT can be combined to return desired results.
You can use the GROUP_CONCAT aggregate function
SELECT manuscript.id, GROUP_CONCAT(manuscript_log.log_number)
FROM manuscript INNER JOIN manuscript_log
ON manuscript.id = manuscript_log.manuscript_id
GROUP BY manuscript.id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With