我试图将一个子查询添加到select中,如下所示:
select ProductID,
ProductName,
concat((UnitsInStock / (select sum(UnitsInStock) from products))*100, '%')
as Percent_of_total_units_in_stock
from products
order by ProductID;
/*
This query returns the same result as query above.
Here 3119 is hardcoded whereas query above uses
subquery to calculate 3119 on the fly.
*/
select ProductID,
ProductName,
concat((UnitsInStock / 3119)*100, '%')
as Percent_of_total_units_in_stock
from products
order by ProductID;
对于以' - '开头的3行,查询运行正常。注意:这只是为了表明问题 - 不会将其包含在实时查询中。
我含糊地理解我试图使用左连接运行内部查询,但这并不起作用。如果能够更好地解释这一点,那就太好了。
问题:有没有办法构建这个查询,所以我可以一起运行2?我尝试了几种配置,但我总是得到MySQL错误:
Unknown column 'a.receiver' in 'on clause'
Error code 1054.
感谢
答案 0 :(得分:1)
我认为你在子查询中引用了错误的别名。注释代码说明:
FROM messages AS aa
JOIN users AS bb ON aa.sender=bb.userid
LEFT JOIN blocked AS cc ON cc.requester=aa.sender AND cc.receiver = a.receiver
LEFT JOIN blocked AS dd ON dd.receiver=aa.receiver AND dd.requester = a.receiver
子查询中的messages
表具有别名aa
。您在out查询中使用a
作为别名。所以我认为这是一个错误,应该是这样的:
FROM messages AS aa
JOIN users AS bb ON aa.sender=bb.userid
LEFT JOIN blocked AS cc ON cc.requester=aa.sender AND cc.receiver = aa.receiver
LEFT JOIN blocked AS dd ON dd.receiver=aa.receiver AND dd.requester = aa.receiver