mysql 内联两个表,匹配月份
mysql inner join two tables with month matching
我在 MySQL 中有两个 table,在同一个数据库中有两个 table。
table名称数据
| service | count | date |
--------------------------------------
| bugss | 375 | 2022-01-01 05:00:00.00000
| fromsite | 5 | 2022-02-01 05:00:00.00000
| kbocetra | 100 | 2022-01-05 07:00:00.00000
已尝试获取数据 table
SELECT SUM(`count`) AS Alertcount,
DATE_FORMAT(`date`, '%M') AS Month,
FROM data
GROUP BY DATE_FORMAT(`date`, '%Y-%m')
输出:
January | 475
February | 5
另一个table名字pd
| group | minutes | projdate |
--------------------------------
gcp | 145 | 2022-01-01 05:00:00.00000
azure | 10 | 2022-02-01 05:00:00.00000
aws | 80 | 2022-01-05 07:00:00.00000
我在下面的命令中尝试了单独的 tables,如下所示的 pd table ..输出为
SELECT SUM(`minutes`) AS Hours,
DATE_FORMAT(`group `, '%M') AS Month
FROM pd
GROUP BY DATE_FORMAT(`group`, '%Y-%m')
输出:
January | 225
February | 10
我希望输出如下所示,总计数将作为两个 tables count/minutes 的输出,即 475/225
和 5/10。
请帮忙,我对内部声明很生气,但没有用。
Month
total
January
0.78
February
2
运行执行以下命令并查看结果。
SELECT
a.`Month`,
a.`Hours` / b.`Alertcount` as 'total'
FROM
(
SELECT
SUM( `minutes` ) AS Hours,
DATE_FORMAT( `group `, '%M' ) AS 'Month'
FROM
pd
GROUP BY
DATE_FORMAT( `group`, '%Y-%m' )
) a
INNER JOIN (
SELECT
SUM( `count` ) AS Alertcount,
DATE_FORMAT( `date`, '%M' ) AS 'Month'
FROM
DATA
GROUP BY
DATE_FORMAT( `date`, '%Y-%m' )
) b ON a.`Month` = b.`Month`
选择表格时,您可以使用除法运算符,如您所见here。
我在 MySQL 中有两个 table,在同一个数据库中有两个 table。 table名称数据
| service | count | date |
--------------------------------------
| bugss | 375 | 2022-01-01 05:00:00.00000
| fromsite | 5 | 2022-02-01 05:00:00.00000
| kbocetra | 100 | 2022-01-05 07:00:00.00000
已尝试获取数据 table
SELECT SUM(`count`) AS Alertcount,
DATE_FORMAT(`date`, '%M') AS Month,
FROM data
GROUP BY DATE_FORMAT(`date`, '%Y-%m')
输出:
January | 475
February | 5
另一个table名字pd
| group | minutes | projdate |
--------------------------------
gcp | 145 | 2022-01-01 05:00:00.00000
azure | 10 | 2022-02-01 05:00:00.00000
aws | 80 | 2022-01-05 07:00:00.00000
我在下面的命令中尝试了单独的 tables,如下所示的 pd table ..输出为
SELECT SUM(`minutes`) AS Hours,
DATE_FORMAT(`group `, '%M') AS Month
FROM pd
GROUP BY DATE_FORMAT(`group`, '%Y-%m')
输出:
January | 225
February | 10
我希望输出如下所示,总计数将作为两个 tables count/minutes 的输出,即 475/225 和 5/10。 请帮忙,我对内部声明很生气,但没有用。
Month | total |
---|---|
January | 0.78 |
February | 2 |
运行执行以下命令并查看结果。
SELECT
a.`Month`,
a.`Hours` / b.`Alertcount` as 'total'
FROM
(
SELECT
SUM( `minutes` ) AS Hours,
DATE_FORMAT( `group `, '%M' ) AS 'Month'
FROM
pd
GROUP BY
DATE_FORMAT( `group`, '%Y-%m' )
) a
INNER JOIN (
SELECT
SUM( `count` ) AS Alertcount,
DATE_FORMAT( `date`, '%M' ) AS 'Month'
FROM
DATA
GROUP BY
DATE_FORMAT( `date`, '%Y-%m' )
) b ON a.`Month` = b.`Month`
选择表格时,您可以使用除法运算符,如您所见here。