开发者

Trying to 'merge' partial data in 2 or more rows with MySql

开发者 https://www.devze.com 2023-04-09 18:43 出处:网络
I have a database with three tables, one with titles, one with authors and a join table 开发者_如何学编程that combines the two. Most of the titles have one author, but some have more than one. I am ab

I have a database with three tables, one with titles, one with authors and a join table 开发者_如何学编程that combines the two. Most of the titles have one author, but some have more than one. I am able to return the rows I want, but the titles with more than one author return a row for each title, one having one author and one with the other author as you might expect. Is there I way I can combine the authors into one row?

Here is my statement

SELECT DISTINCT t.id, t.title, t.notes, 
a.lastname, a.firstname, a.middlename
FROM titles t, author a, authortitle at
WHERE  t.title LIKE '".$letter."%'
AND at.author_id_fk = a.id
AND at.title_id_fk = t.id

$letter is the first letter of the title.


Try this;

SELECT t.id, t.title, t.notes, 
GROUP_CONCAT( CONCAT(a.lastname, a.firstname, a.middlename) SEPARATOR ', ') AS author
FROM titles t, author a, authortitle at
WHERE  t.title LIKE '".$letter."%'
AND at.author_id_fk = a.id
AND at.title_id_fk = t.id
GROUP BY t.id
0

精彩评论

暂无评论...
验证码 换一张
取 消