在mysql中计算多个查询
我有3个问题,第一个是这样的在mysql中计算多个查询,mysql,Mysql,我有3个问题,第一个是这样的 SELECT amount FROM table1 WHERE id = "1" 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") 这个的输出是 amount 10000 然后是我的第二
SELECT amount FROM table1
WHERE id = "1"
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")
这个的输出是
amount
10000
然后是我的第二个问题
SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1"
输出为
hours
400
然后是我的第三个问题
SELECT totalcost FROM table3
WHERE id = "1"
SELECT 'SUMQ2Q3',
(SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1")
+
(SELECT totalcost FROM table3
WHERE id = "1")
输出
totalcost
5000
我添加了第二个和第三个查询的值
SELECT totalcost FROM table3
WHERE id = "1"
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 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")
正确的方法是什么?试一试怎么样
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")
不幸的是这里没有DB,但是
SELECT t1.amount - t2.sum + t3.totalcost
FROM (SELECT amount FROM table1 WHERE id = "1") as t1,
(SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1") as t2
(SELECT totalcost FROM table3 WHERE id = "1") as t3
也许能胜任 只需编写
其中id=1
进行整数比较。我就是这样做的,只是将其更改为选择t1.amount-(t2.sum+t3.totalcost)
有效。谢谢D
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