我有两张桌子:
表1:
表2:
我想要做的是在查询中添加hasil2 + hasil1:
SELECT (hasil2+hasil1) FROM Table1,Table2
结果是:
错误结果:
但结果应该是这样的: 0.7015 0.7792 0.5517
我已完成以下帖子:How to add different columns from different tables 但仍然有错误..
那么我应该如何编写查询?
答案 0 :(得分:2)
使用联接:
SELECT
t1.kd, t1.alt, t1.hasil2 + t2.hasil1 AS hasil
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.kd = t2.kd AND t1.alt = t2.alt;
或者,如果alt
实际上不是连接列,则:
SELECT
t1.kd, t1.alt, t1.hasil2 + t2.hasil1 AS hasil
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.kd = t2.kd;
答案 1 :(得分:1)
我认为在简单join
的帮助下,您可以实现所需的输出:
SELECT t1.kd, (t1.hasil2+t2.hasil1) AS result
FROM table1 t1
INNER JOIN table2 t2 ON t2.kd = t1.kd
答案 2 :(得分:1)
您可以尝试加入 -
SELECT (t1.hasil2+t2.hasil1) AS result FROM table1 t1 JOIN table2 t2 ON t2.kd = t1.kd
答案 3 :(得分:-1)
请尝试此查询。返回kd,alt和hasil1和hasil2之和,并按kd列对结果表的顺序进行分组。
SELECT table1.kd, table1.alt, SUM(table1.hasil2 + table2.hasil1) AS Total
FROM table1, table2
WHERE table1.alt = table2.alt
GROUP BY table1.kd
你会有
kd | alt | Total
-------------
1 | a1 | 0.7015
2 | a2 | 0.7792
3 | a3 | 0.5517