在mysql中计算多个查询

时间:2014-03-18 19:11:29

标签: mysql

我有3个查询,第一个是这样的

SELECT amount FROM table1
WHERE id = "1"

这是

的输出
amount
10000

然后是我的第二个查询

SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1"

输出

hours
400

然后是我的第三个查询

SELECT totalcost FROM table3
WHERE id = "1"

输出

totalcost
5000

我添加了第二个和第三个查询的值

SELECT 'SUMQ2Q3',
(SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1")

+

(SELECT totalcost FROM table3
WHERE id = "1")

并得到了

的输出
5400

我想要发生的下一件事是将第一个查询的值减去我对第二个和第三个查询求和时得到的输出。 但我继续得到语法错误。它目前看起来像这样

SELECT amount FROM table1
WHERE id = "1"

-

SELECT
(SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1"

+

SELECT totalcost FROM table3
WHERE id = "1")

如何正确地做到这一点?

2 个答案:

答案 0 :(得分:0)

如何尝试

SELECT
    (SELECT amount FROM table1 WHERE id = "1")
    - (SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1")
    - (SELECT totalcost FROM table3 WHERE id = "1")

答案 1 :(得分:0)

SELECT (
(
SELECT amount FROM table1 WHERE id = "1") As x -
(SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1") As y +
(SELECT totalcost FROM table3 WHERE id = "1") As z
)
As answer