计算 2 行 2 个不同表的平均值
Calculate an average with 2 rows of 2 different tables
我是 MySQL 的新手,目前我正在努力解决一个问题。
我有 2 个表(techlog 和 sales)。我现在打算做的是,从 techlog 的列中得出一个总和,与销售相同。这两个总和我需要计算新的 Sum(sales.row) / Sum(techlog.row)。这样做的想法是我可以获得每分钟的价格。
有可能吗?
两个表共享具有相同 ID 的列。此外,一些列(例如入学考试)也可用于两个表格。
到目前为止我尝试过的:
use dbm_project;
SELECT techlog.Immatriculation, sum(techlog.TimeTOT), sum(sales.total)
FROM techlog
INNER JOIN sales
ON techlog.Immatriculation = sales.Immatriculation
GROUP BY techlog.Immatriculation
我提前道歉,因为我发现很难用缺少的经验来表达我的问题。
长话短说:我想计算两个不同表的两个值,我想用它们计算一些新的东西。
这是一个子查询作业。
一个是
SELECT Immatriculation, sum(TimeTOT) TimeTot
FROM techlog
GROUP BY Immatriculation
另一个是
SELECT Immatriculation, sum(total) SalesTot
FROM sales
GROUP BY Immatriculation
然后你加入他们
SELECT t.Immatriculation, t.TimeTot, s.SalesTot
FROM (
SELECT Immatriculation, sum(TimeTOT) TimeTot
FROM techlog
GROUP BY Immatriculation
) t
LEFT JOIN (
SELECT Immatriculation, sum(total) SalesTot
FROM sales
GROUP BY Immatriculation
) s ON t.Immatriculation = s.Immatriculation;
之所以有效,是因为....
- 子查询可以代替表。那就是 S in S 结构 Query Language .
- 两个子查询中的每一个都为每个
Immatriculation
值一行。所以你在你的结果集中得到一行。
我是 MySQL 的新手,目前我正在努力解决一个问题。
我有 2 个表(techlog 和 sales)。我现在打算做的是,从 techlog 的列中得出一个总和,与销售相同。这两个总和我需要计算新的 Sum(sales.row) / Sum(techlog.row)。这样做的想法是我可以获得每分钟的价格。 有可能吗?
两个表共享具有相同 ID 的列。此外,一些列(例如入学考试)也可用于两个表格。
到目前为止我尝试过的:
use dbm_project;
SELECT techlog.Immatriculation, sum(techlog.TimeTOT), sum(sales.total)
FROM techlog
INNER JOIN sales
ON techlog.Immatriculation = sales.Immatriculation
GROUP BY techlog.Immatriculation
我提前道歉,因为我发现很难用缺少的经验来表达我的问题。
长话短说:我想计算两个不同表的两个值,我想用它们计算一些新的东西。
这是一个子查询作业。
一个是
SELECT Immatriculation, sum(TimeTOT) TimeTot
FROM techlog
GROUP BY Immatriculation
另一个是
SELECT Immatriculation, sum(total) SalesTot
FROM sales
GROUP BY Immatriculation
然后你加入他们
SELECT t.Immatriculation, t.TimeTot, s.SalesTot
FROM (
SELECT Immatriculation, sum(TimeTOT) TimeTot
FROM techlog
GROUP BY Immatriculation
) t
LEFT JOIN (
SELECT Immatriculation, sum(total) SalesTot
FROM sales
GROUP BY Immatriculation
) s ON t.Immatriculation = s.Immatriculation;
之所以有效,是因为....
- 子查询可以代替表。那就是 S in S 结构 Query Language .
- 两个子查询中的每一个都为每个
Immatriculation
值一行。所以你在你的结果集中得到一行。