我有2个表,我想从单个查询中获取数据集以在图表中显示它。 但我在执行中遇到错误。只有年份和expYear以及Month和Exp Month这些列可能具有共同数据。每一行都有其年份和月份字段。
这是我正在尝试的查询:
select
t.transExpense
g.grocExpense
from
(Select sum(traexpense) as transExpense from
transportExpense where Year="2014") t,
(Select sum(groexpense) as grocExpense from
groceryExpense where expYear="2014") g
请帮忙。
答案 0 :(得分:2)
在所选字段之间添加逗号:
select
t.transExpense, -- << right there
g.grocExpense
from
(Select sum(traexpense) as transExpense from transportExpense where Year="2014") t,
(Select sum(groexpense) as grocExpense from groceryExpense where expYear="2014") g
答案 1 :(得分:2)
您在t.transExpense
之后缺少逗号。
但是,要返回单个值,您不需要将子查询放入FROM子句中;您可以在SELECT中直接使用它们作为标量子查询:
SELECT (SELECT SUM(traexpense)
FROM transportExpense
WHERE Year = '2014'
) AS transExpense,
(SELECT SUM(groexpense)
FROM grocExpense
WHERE expYear = '2014'
) AS grocExpense;
答案 2 :(得分:0)
可以使用JOIN Query解决。类似的东西:
SELECT sum(t.traexpense) as t.transExpense, sum(g.groexpense) as g.grocExpense
from transportExpense t LEFT JOIN groceryExpense g ON t.fieldname=g.fieldname
where g.expYear=2014 and t. Year=2014
如果您的牌桌没有任何加入的公共字段,您可以使用t.Year和g.expYear加入。