这是我目前的查询以获取我的优惠:
SELECT deals.*, SUM(orders.cost) AS 'revenue' FROM deals
LEFT JOIN orders ON (orders.deal_id = deals.id)
WHERE 1
AND (orders.status = 'new' OR orders.status = 'delivered')
GROUP BY deals.ID
ORDER BY revenue ASC
现在这很好用,我可以按顺序排序这个查询(按顺序)我希望在订单或交易表中的任何字段。
然后当我遍历交易并用foreach()显示它们时, 我有这个查询来抓住客户的平均评论明星的交易:
SELECT ROUND(AVG(stars)) as 'avg_stars' FROM deals_reviews WHERE deal_id = *THE DEAL_ID* AND active = '1'
现在我想从平均星中排除ASC / DESC的所有交易。
要做到这一点,我需要将该查询与上面的查询合并,当我需要显示avg_stars时,我从第一个也是唯一的查询而不是第二个查询中获取此值。
如何将该查询'合并'到第一个?那么当这样做的时候我们就可以做ORDER BY avg_stars DESC并且它会按此排序吗?
答案 0 :(得分:0)
我不确定我是否理解正确,但这应该是解决方案
SELECT deals.*, SUM(orders.cost) AS 'revenue' FROM deals
LEFT JOIN orders ON (orders.deal_id = deals.id)
LEFT JOIN deals_reviews ON (deals.id = deals_reviews.deal_id AND active = '1')
WHERE 1
AND (orders.status = 'new' OR orders.status = 'delivered')
GROUP BY deals.ID
ORDER BY ROUND(AVG(stars)) as 'avg_stars' ASC