我希望能够在单个mysql查询中选择不同的列。我知道这很容易做到的是sql,但你怎么做的是MySQL?
SELECT
MyfirstResult = (select sum(Counted * PartCost) FROM tblParts WHERE resultofcount < 0),
MySecondresult = (select sum(Counted * PartCost) FROM tblParts WHERE resultofcount > 0)
该查询适用于SQL,但如何将相同的功能移植到MySQl
在这种情况下,我的结果应该是1000 | 2000。一行有2列。
答案 0 :(得分:2)
差不多;
SELECT
(select sum(Counted * PartCost) FROM tblParts WHERE resultofcount < 0) MyfirstResult,
(select sum(Counted * PartCost) FROM tblParts WHERE resultofcount > 0) MySecondresult;
演示here。
你也可以在MySQL中稍微简单一些;
SELECT sum(Counted * PartCost*(resultofcount < 0)) Myfirstresult,
sum(Counted * PartCost*(resultofcount > 0)) MysecondResult
FROM tblParts
演示here。
答案 1 :(得分:0)
稍微改变你的语法。列别名遵循计算的表达式或通过AS aliasname
进行子选择。
SELECT
(select sum(Counted * PartCost) FROM tblParts WHERE resultofcount < 0) AS MyFirstResult,
(select sum(Counted * PartCost) FROM tblParts WHERE resultofcount > 0) AS MySecondResult