我有这样的查询:
SELECT DISTINCT
obl_books.book_id,
obl_books.long_title,
obl_books.short_title,
obl_authors.first_name
FROM
obl_books,
obl_authors,
books_authors
WHERE
obl_books.book_id = books_authors.book_id
AND
obl_authors.author_id = books_authors.author_id
AND
obl_books.short_title = 'SQL'
它为每个作者提供了2个单独的行。我希望它像这样一行:
Book1| SQL REFERENCE | author1 | author2 | author3
如何直接在SQL查询中实现它或者通过对ResultSet结果执行某些操作?请指导。请告诉我没有任何PLSQL机制。
答案 0 :(得分:1)
如果你正在使用MySQL,请试试这个:
SELECT DISTINCT
obl_books.book_id,
obl_books.long_title,
obl_books.short_title,
GROUP_CONCAT(obl_authors.first_name, ',')
ETC...
使用您想要的任何内容更改分隔符(逗号)
答案 1 :(得分:0)
取决于哪个rdbms,但您可以尝试
SELECT DISTINCT obl_books.book_id||obl_books.long_title|| etc...
或者在帮助
中查找'Concat()'答案 2 :(得分:0)
JOIN
代替旧的SQL-89隐式连接WHERE
语法DISTINCT
GROUP BY
使用GROUP_CONCAT()
连接一列中的所有名称:
SELECT
b.book_id,
b.long_title,
b.short_title,
GROUP_CONCAT(a.first_name
ORDER BY a.first_name
SEPARATOR ', '
) AS first_names
FROM
obl_books AS b
JOIN
books_authors AS ba
ON b.book_id = ba.book_id
JOIN
obl_authors AS a
ON a.author_id = ba.author_id
WHERE
b.short_title = 'SQL'
GROUP BY
b.book_id,
b.long_title,
b.short_title ;