MySQL GROUP_CONCAT() 在查询中的 LIKE 约束后生成部分答案
MySQL GROUP_CONCAT() produces partial answer after a LIKE constraint in query
select b.book_id,isbn,title,description,book_count,group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where b.book_id = ab.book_id and a.author_id = ab.author_id
and (a.firstname like '%abc%') group by isbn;
以上代码产生,
1001 | 1234567890123 | C Programming | Good book for beginners | 10 | abcd
但正确答案是多位作者,(包括 efgh、ijkl 作者)
1001 | 1234567890123 | C Programming | Good book for beginners | 10 | abcd, efgh, ijkl
1002 | 1234567890111 | Java | Good book for Java Programmers | 5 | xyz, uvw
...
...
这是在没有使用 LIKE 约束的情况下实现的,
select b.book_id,isbn,title,description,book_count,group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where b.book_id = ab.book_id and a.author_id = ab.author_id group by isbn;
如何达到预期的输出?
其他作者已被删除,因为您对作者姓名的限制仍然适用,即使您已将它们用于 select 您想要的书籍。
使用您当前的作者过滤器 return 图书 ID 列表,然后将其用作子查询 WHERE bookId IN (SELECT ...)
来过滤您想要的图书,然后使用 author_book 再次加入和 author 获取 selected 书籍的所有作者。
尝试这样的事情:
select b.book_id,isbn,title,description,book_count,
group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where
b.book_id = ab.book_id
and a.author_id = ab.author_id
and b.book_id IN (
SELECT ab.book_id
from author a, author_book ab
where a.author_id = ab.author_id and (a.firstname like '%abc%')
)
group by isbn;
您可以两次使用联接 table,一次按作者过滤,第二次列出相关书籍的所有作者;并且只用一个 select 子句就可以做到这一点。
nb:我假设 book_count
来自书籍 table。
SELECT
b.book_id
, b.isbn
, b.title
, b.description
, b.book_count
, group_concat(concat_ws(' ', a2.firstname, a2.middlename, a2.lastname)) AS authors
FROM author AS a1
INNER JOIN author_book AS ab ON a1.author_id = ab.author_id
INNER JOIN book AS b ON b.book_id = ab.book_id
INNER JOIN author AS a2 ON a2.author_id = ab.author_id
WHERE a1.lastname LIKE 'a%'
GROUP BY
b.book_id
, b.isbn
, b.title
, b.description
, b.book_count
;
- 请注意,您应该停止在 table 个名称之间使用逗号
from 子句可帮助您采用显式连接语法。
- 此外,您应该始终(无一例外)参考每一列
使用相关的 table 或别名作为前缀。
- 始终列出 group by 子句中的所有非聚合列。 (MySQL 有一个 "lazy syntax" 只能接受一个列 - 但它可能会失败,具体取决于服务器设置。)
IF实际计算出book_count
则:
SELECT
b.book_id
, b.isbn
, b.title
, b.description
, COUNT(DISTINCT b.book_id) AS book_count
, group_concat(concat_ws(' ', a2.firstname, a2.middlename, a2.lastname)) AS authors
FROM author AS a1
INNER JOIN author_book AS ab ON a1.author_id = ab.author_id
INNER JOIN book AS b ON b.book_id = ab.book_id
INNER JOIN author AS a2 ON a2.author_id = ab.author_id
WHERE a1.lastname LIKE 'a%'
GROUP BY
b.book_id
, b.isbn
, b.title
, b.description
;
select b.book_id,isbn,title,description,book_count,group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where b.book_id = ab.book_id and a.author_id = ab.author_id
and (a.firstname like '%abc%') group by isbn;
以上代码产生,
1001 | 1234567890123 | C Programming | Good book for beginners | 10 | abcd
但正确答案是多位作者,(包括 efgh、ijkl 作者)
1001 | 1234567890123 | C Programming | Good book for beginners | 10 | abcd, efgh, ijkl
1002 | 1234567890111 | Java | Good book for Java Programmers | 5 | xyz, uvw
...
...
这是在没有使用 LIKE 约束的情况下实现的,
select b.book_id,isbn,title,description,book_count,group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where b.book_id = ab.book_id and a.author_id = ab.author_id group by isbn;
如何达到预期的输出?
其他作者已被删除,因为您对作者姓名的限制仍然适用,即使您已将它们用于 select 您想要的书籍。
使用您当前的作者过滤器 return 图书 ID 列表,然后将其用作子查询 WHERE bookId IN (SELECT ...)
来过滤您想要的图书,然后使用 author_book 再次加入和 author 获取 selected 书籍的所有作者。
尝试这样的事情:
select b.book_id,isbn,title,description,book_count,
group_concat(concat_ws(' ',a.firstname,a.middlename,a.lastname)) as authors
from book b, author a, author_book ab
where
b.book_id = ab.book_id
and a.author_id = ab.author_id
and b.book_id IN (
SELECT ab.book_id
from author a, author_book ab
where a.author_id = ab.author_id and (a.firstname like '%abc%')
)
group by isbn;
您可以两次使用联接 table,一次按作者过滤,第二次列出相关书籍的所有作者;并且只用一个 select 子句就可以做到这一点。
nb:我假设 book_count
来自书籍 table。
SELECT
b.book_id
, b.isbn
, b.title
, b.description
, b.book_count
, group_concat(concat_ws(' ', a2.firstname, a2.middlename, a2.lastname)) AS authors
FROM author AS a1
INNER JOIN author_book AS ab ON a1.author_id = ab.author_id
INNER JOIN book AS b ON b.book_id = ab.book_id
INNER JOIN author AS a2 ON a2.author_id = ab.author_id
WHERE a1.lastname LIKE 'a%'
GROUP BY
b.book_id
, b.isbn
, b.title
, b.description
, b.book_count
;
- 请注意,您应该停止在 table 个名称之间使用逗号 from 子句可帮助您采用显式连接语法。
- 此外,您应该始终(无一例外)参考每一列 使用相关的 table 或别名作为前缀。
- 始终列出 group by 子句中的所有非聚合列。 (MySQL 有一个 "lazy syntax" 只能接受一个列 - 但它可能会失败,具体取决于服务器设置。)
IF实际计算出book_count
则:
SELECT
b.book_id
, b.isbn
, b.title
, b.description
, COUNT(DISTINCT b.book_id) AS book_count
, group_concat(concat_ws(' ', a2.firstname, a2.middlename, a2.lastname)) AS authors
FROM author AS a1
INNER JOIN author_book AS ab ON a1.author_id = ab.author_id
INNER JOIN book AS b ON b.book_id = ab.book_id
INNER JOIN author AS a2 ON a2.author_id = ab.author_id
WHERE a1.lastname LIKE 'a%'
GROUP BY
b.book_id
, b.isbn
, b.title
, b.description
;