Mysql:将视图创建为dailyview,以计算具有特定机器ID的表中的行数

huwehgph  于 2022-12-26  发布在  Mysql
关注(0)|答案(1)|浏览(158)

我想创建一个视图来计算用水量,我有三个微控制器在3个不同的地方发送数据从3个传感器。
我有一个数据库与2个相关的表:第一个名为machine,带有id,例如:

id_machinen = { "sensor1": 1, "sensor2": 2, "sensor3": 3 }

第二个称为verbrauch,用于保存Ucontrollers发送的数据。
我在mysql里试过这个:

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `dailyview` AS
    SELECT 
        `verbrauch`.`machine` AS `machine`,
        CONCAT(YEAR(`verbrauch`.`timestamp`),
                '/',
                DAY(`verbrauch`.`timestamp`, 0)) AS `day`,
        SUM(`verbrauch`.`verbrauch`) AS `sum`
    FROM
        `verbrauch`
    GROUP BY `verbrauch`.`machine` , CONCAT(YEAR(`verbrauch`.`timestamp`),
            '/',
            DAY(`verbrauch`.`timestamp`, 0))
enyaitl3

enyaitl31#

函数day/dayofmonth只有一个参数-https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_dayofmonth

CREATE 
    ALGORITHM = UNDEFINED 
    #DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `dailyview` AS
    SELECT 
        `verbrauch`.`machine` AS `machine`,
        CONCAT(YEAR(`verbrauch`.`timestamp`),
                '/',
                DAY(`verbrauch`.`timestamp`)) AS `day`,
        SUM(`verbrauch`.`verbrauch`) AS `sum`
    FROM
        `verbrauch`
    GROUP BY `verbrauch`.`machine` , CONCAT(YEAR(`verbrauch`.`timestamp`),
            '/',
            DAY(`verbrauch`.`timestamp`))

相关问题