0

我正在使用的 Mysql 版本不允许我在子查询中使用 LIMIT,因此我可以加快查询速度。如何在下一个查询中使用 join 代替?

SELECT p . * , (

SELECT AVG( review_rating ) AS rating_total
FROM reviews r
WHERE r.review_item_ref = p.pattern_ref
AND r.review_state =1
GROUP BY r.review_item_ref
) AS review_rating
FROM patterns p
WHERE pattern_active >=0
AND p.pattern_family =27
AND p.pattern_ref
IN (

SELECT item_pattern_ref
FROM items
WHERE item_pattern_ref = p.pattern_ref LIMIT 1
)
GROUP BY p.pattern_id
ORDER BY p.pattern_description ASC , LCASE( p.pattern_description ) ASC
LIMIT 0 , 16
4

2 回答 2

0

看起来您正在尝试验证 p.pattern_ref 在项目中是否匹配,对吗?

如果是这样的话,

SELECT 
    p . *,
    (SELECT AVG( review_rating ) AS rating_total
        FROM reviews r
        WHERE r.review_item_ref = p.pattern_ref
            AND r.review_state =1
        GROUP BY r.review_item_ref
    ) AS review_rating
FROM patterns p
INNER JOIN items i ON p.pattern_ref = i.item_pattern_ref
WHERE p.pattern_active >= 0
    AND p.pattern_family = 27
GROUP BY p.pattern_id
ORDER BY p.pattern_description ASC , LCASE( p.pattern_description ) ASC
LIMIT 0 , 16

应该管用。INNER JOIN 只会返回双方都匹配的行。

于 2013-02-10T04:28:09.670 回答
0

您不能在子查询中使用 LIMIT。

于 2013-02-10T18:32:05.507 回答