我有以下表格
书桌
id title authors 3 Linux
作者表
id firstname lastname fk 1 name name 3 2 name2 name 3
我想查询authors表,使用GROUP_CONCAT()
获取作者列填充此表的内容id title authors 3 Linux name name | name2 name2
答案 0 :(得分:0)
对于您知道其ID的单本图书,您可以将作者列表作为单个值获取,如下所示:
SELECT group_concat(firstname || ' ' || lastname)
FROM authors
WHERE fk = ?;
只需在实际查询中将其用作correlated subquery:
SELECT id,
title,
(SELECT group_concat(firstname || ' ' || lastname, ' | ')
FROM authors
WHERE fk = book.id
) AS authors
FROM book;