在单个查询中评估特定的分组值?

Posted

技术标签:

【中文标题】在单个查询中评估特定的分组值?【英文标题】:Evaluate specific grouped values in a single query? 【发布时间】:2012-03-30 05:45:29 【问题描述】:

我有一个书籍数据库,其中包含与书籍相关的类别和标签。我正在尝试构建一个过滤器,让用户可以过滤多个类别或标签,并查看具有这些标签或类别的书籍。它适用于单个类别或标签,但如果由于按图书 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

我尝试过使用 GROUP_CONCAT 和 HAVING,我刚刚阅读了有关 IN() 的信息,但我尝试过,但它不起作用。我现在真正需要做的就是对其执行 IN() 之类的操作,有什么建议吗?

编辑:我使用它并且能够获得单个类别的结果,但它只能工作一次...... 这行得通...

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

这不是

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

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

【问题讨论】:

【参考方案1】:

不久前有 *** 帖子。请check it out。 另一种方法是每次创建临时表并将所有标签和类别放在那里,然后进行连接。

【讨论】:

我尝试按照您准确链接的帖子的答案进行操作,但它不起作用。我在 mysql 中遇到各种错误。我也不确定如何按照您的建议创建临时表。【参考方案2】:

我放弃了尝试单独使用 JOIN 并使用子查询。这对我有用,可能并不理想,但我现在可以根据需要过滤尽可能多的标签或类别。

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

这是通过检查任何条件是否存在任何结果,然后计算条件匹配的数量来实现的。在外部查询中,我将两个联合查询相加,并确定这些计数的 SUM() 是否与参数总数匹配。

【讨论】:

以上是关于在单个查询中评估特定的分组值?的主要内容,如果未能解决你的问题,请参考以下文章

针对 LINQ To SQL 查询中的位字段评估布尔值

如何让 heapq 评估特定属性的堆?

在 if 语句中评估单个字符:C++

组合框的访问选择查询并不总是评估复选框值

查询错误中的 VBA 评估 IIF 的错误部分

逐行输入评估与操作员以及如何存储更改值,直到用户键入“。”