我試圖獲得已定義數量的產品已銷售至少6次,但mysql給了我一個未知的列,但它是由相同的價值排序,如果價值取出WHERE子句。未知的列MYSQL
這是我的查詢。
SELECT
ps.product_id,
(SELECT IFNULL(SUM(s.product_quantity),0) FROM product_stock s WHERE ps.product_id = s.product_id AND s.product_quantity > 0) AS stock,
SUM(ps.product_quantity) AS vendidos
FROM product_sold ps
WHERE
ps.product_sold_approved_time >= 1419786999
AND
vendidos >= 6
GROUP BY ps.product_id
ORDER BY vendidos DESC
這種方法更好,然後做一個子查詢的where子句?這樣 SELECT ps.product_id, (SELECT IFNULL(SUM(s.product_quantity),0)FROM product_stock S其中ps.product_id = s.product_id AND s.product_quantity> 0)AS股票, SUM(PS。 product_quantity)AS vendidos FROM product_sold PS WHERE (SELECT SUM(p.product_quantity)FROM product_sold p其中p.product_sold_approved_time> = 1419786999 AND p.product_id = ps.product_id)> = 6和 ps.product_sold_approved_time> = 1419786999 GROUP BY ps.product_id ORDER BY vendidos DESC – bmacuer 2015-04-05 18:58:40
這取決於,對於大型數據集來說,明智的做法是避免子查詢,並在正確使用的列上有適當的索引。但是,當你問到它的主觀性,完全取決於數據量並且使用解釋計劃時,可以分析更多關於性能的信息。 – 2015-04-05 19:02:22