Mysql 是否在单个查询中计算特定的分组值?

Mysql 是否在单个查询中计算特定的分组值?,mysql,Mysql,我有一个图书数据库,其中包含与图书相关的类别和标签。我试图建立一个过滤器,让用户过滤多个类别或标签,并看到有这些标签或类别的书籍。它可以与单个类别或标记一起使用,但如果存在多个标记或类别,则会因按图书id分组而崩溃 SELECT books.id as id,title, sub_title,medium_image_url, amzn_url,amzn_review as review,SUBSTRING(kindle_price,2) as price, IFNULL(ROU

我有一个图书数据库,其中包含与图书相关的类别和标签。我试图建立一个过滤器,让用户过滤多个类别或标签,并看到有这些标签或类别的书籍。它可以与单个类别或标记一起使用,但如果存在多个标记或类别,则会因按图书id分组而崩溃

SELECT books.id as id,title, sub_title,medium_image_url,
    amzn_url,amzn_review as review,SUBSTRING(kindle_price,2) as price,
    IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) AS rating ,
    categories.category as category,GROUP_CONCAT(DISTINCT categories.id SEPARATOR ", ") as all_cats 
    FROM books 
    JOIN combined_ratings ON books.id = combined_ratings.book_id JOIN book_categories ON books.id = book_categories.book_id 
    JOIN categories ON book_categories.category_id = categories.id 
    WHERE (SUBSTRING(kindle_price,2) >= 0 
    AND SUBSTRING(kindle_price,2) <= 12 
    AND kindle_price <> "") 
    AND (IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) >= 0 
    AND IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) <= 100) 
    GROUP BY id 
    ORDER BY price ASC, rating DESC
这是不可能的

AND FIND_IN_SET( categories.id, '6' ) > 0 AND FIND_IN_SET( categories.id, '5' ) > 0 

如果我能让它工作,它就解决了我的问题。

不久前有一篇stackoverflow文章。请
另一种方法是每次创建临时表,将所有标记和类别放在那里,然后进行联接。

我放弃了单独使用联接的尝试,转而使用子查询。这对我来说很有效,可能不太理想,但我现在可以根据需要过滤任意多的标签或类别

SELECT id, title, author, image, sub_title, price, SUM(count) as count, rating FROM 
(
SELECT books.id as id, title, author, medium_image_url as image, sub_title, kindle_price as price, IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) as rating , COUNT( DISTINCT category_id ) AS count 
FROM book_categories 
JOIN books ON book_categories.book_id = books.id 
JOIN authors ON books.author_id = authors.id 
JOIN combined_ratings ON books.id = combined_ratings.book_id 
WHERE category_id = 5 
OR category_id = 6 
OR category_id = 12 
GROUP BY book_categories.book_id 
HAVING count >= 3 
UNION ALL 
SELECT books.id as id, title, author, medium_image_url as image, sub_title, kindle_price as price, IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) as rating , COUNT( DISTINCT tag_id ) AS count 
FROM book_tags JOIN books ON book_tags.book_id = books.id 
JOIN authors ON books.author_id = authors.id 
JOIN combined_ratings ON books.id = combined_ratings.book_id 
WHERE book_tags.tag_id = 22 
GROUP BY book_tags.book_id HAVING count >= 1 
) 
as b1 
WHERE (SUBSTRING(price,2) >= 0 
AND SUBSTRING(price,2) <= 20 AND price <> '') 
AND (rating >= 0 AND rating <= 100) 
GROUP BY id 
HAVING count = 4
ORDER BY price ASC, 
rating DESC

其工作原理是检查任何条件是否存在任何结果,然后计算条件匹配的数量。在外部查询中,我将两个联合查询相加,并确定这些计数的总和是否与参数总数匹配。

我尝试按照您链接的帖子的答案进行操作,但没有成功。我在MySQL中遇到各种各样的错误。我也不知道如何按照您的建议创建临时表。
SELECT id, title, author, image, sub_title, price, SUM(count) as count, rating FROM 
(
SELECT books.id as id, title, author, medium_image_url as image, sub_title, kindle_price as price, IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) as rating , COUNT( DISTINCT category_id ) AS count 
FROM book_categories 
JOIN books ON book_categories.book_id = books.id 
JOIN authors ON books.author_id = authors.id 
JOIN combined_ratings ON books.id = combined_ratings.book_id 
WHERE category_id = 5 
OR category_id = 6 
OR category_id = 12 
GROUP BY book_categories.book_id 
HAVING count >= 3 
UNION ALL 
SELECT books.id as id, title, author, medium_image_url as image, sub_title, kindle_price as price, IFNULL(ROUND(combined_ratings.love/(combined_ratings.love+combined_ratings.hate)*100), 0) as rating , COUNT( DISTINCT tag_id ) AS count 
FROM book_tags JOIN books ON book_tags.book_id = books.id 
JOIN authors ON books.author_id = authors.id 
JOIN combined_ratings ON books.id = combined_ratings.book_id 
WHERE book_tags.tag_id = 22 
GROUP BY book_tags.book_id HAVING count >= 1 
) 
as b1 
WHERE (SUBSTRING(price,2) >= 0 
AND SUBSTRING(price,2) <= 20 AND price <> '') 
AND (rating >= 0 AND rating <= 100) 
GROUP BY id 
HAVING count = 4
ORDER BY price ASC, 
rating DESC