我正在尝试提供与此类似的解决方案-我拥有的是 JOIN 中的 SELECT 查询,问题是该查询对所有行都运行满(我说的是每个表 60,000 行-它运行在 3 张桌子上!)。
所以我想要做的是在 JOIN 中的 SELECT 中添加一个 WHERE 子句。但是,我无法访问外部 SELECT 并获得我需要的正确 WHERE 条件。我正在尝试的查询在这里:
SELECT c.compete_id AS id,
s.id AS store_id,
c.enabled AS enabled,
s.store_name AS store_name,
s.store_url AS store_url,
c.verified AS verified,
r.rating_total AS rating,
r.positive_percent AS percent,
r.type AS type
FROM compete_settings c
LEFT JOIN stores s
ON c.compete_id = s.id
LEFT JOIN (
(SELECT store_id, rating_total, positive_percent, 'ebay' AS type FROM ebay_sellers WHERE store_id = c.compete_id)
UNION
(SELECT store_id, rating_total, positive_percent, 'amazon' AS type FROM amazon_sellers WHERE store_id = c.compete_id)
UNION
(SELECT store_id, CASE WHEN rank = 0 THEN NULL ELSE (200000 - rank) END AS rating_total, '100' as positive_percent, 'alexa' AS type FROM alexa_ratings WHERE store_id = c.compete_id)
) AS r
ON c.compete_id = r.store_id
WHERE c.store_id = :store_id
注意,:store_id
是一个通过框架绑定的变量 - 让我们想象它是 number 12345
。
我怎样才能做到这一点?有任何想法吗?