douhoushou8385 2014-08-10 08:18
浏览 43
已采纳

将mysql查询的结果插入现有表中

I have a an existing table with timestamps and other values and I wanted to create dummy timestamps for each day in the table

For example

    timestamp           phase_1 phase_2 phase_3
2014-03-04 12:00:00   0       0        0
2014-03-05 02:00:00   0       0        0
2014=03-06 01:00:00   0       0        0
2014-03-07 00:00:00   0       3        1

should result to

timestamp           phase_1 phase_2 phase_3
2014-03-04 00:00:00   0       0        0     --<< new
2014-03-04 12:00:00   0       0        0
2014-03-05 00:00:00   0       0        0     --<< new
2014-03-05 02:00:00   0       0        0
2014-03-06 00:00:00   0       0        0     --<< new
2014-03-06 01:00:00   0       0        0
2014-03-07 00:00:00   0       3        1

The following query works fine

`select * from Table1

right join

(select
        `timestamp`
      , phase_1
      , phase_2
      , phase_3
from Table1

union

select
        date(`timestamp`)
      , 0
      , 1
      , 2
from Table1

order by 
        `timestamp`) `

but I am not able to insert the result into the exisiting database

using the following query

`INSERT into Table1

select * from Table1

right join

(select
        `timestamp`
      , phase_1
      , phase_2
      , phase_3
from Table1

union

select
        date(`timestamp`)
      , 0
      , 1
      , 2
from Table1

order by 
        `timestamp`) `

Which i think is correct but i get a

: Every derived table must have its own alias:  error which i am not sure on how to solve

Here is the fiddle enter link description here

Also, will it be possible to insert dummy timestamp and values for days which are not there at all in the table?

Suppose there are two days between the first and last rows in the table and there are no timestamps for those days in the table, will it be possible to add those two dates into the table.

I want to avoid trigger and procedures if possible as I am not at all familiar with them and since the table is dynamically generated, i think it wont be possible to use them.

  • 写回答

2条回答 默认 最新

  • douzhi6160 2014-08-10 08:35
    关注

    You've got to use an alias name for your UNION, but you've got to provide an JOIN condition for your right join too. Probably you want to use:

    INSERT INTO Table1
    select a.* from Table1    
    right join    
    (
        select
            `timestamp`
          , phase_1
          , phase_2
          , phase_3
        from Table1
    
        union
    
        select
            date(`timestamp`)
          , 0
          , 1
          , 2
        from Table1
    
        order by 
            `timestamp`
    ) as a
    ON DATE(a.timestamp) = DATE(Table1.timestamp)
    

    Remarks:

    What is the difference between Left, Right, Outer and Inner Joins? provides a very good explanation of the different joins.

    An outer join needs a join condition, an inner join without a condition is the same as an CROSS JOIN, the cartesian product of both tables (most times one wants to avoid this - but not ever).

    If you use a SELECT statement, that works perfectly as SELECT statement alone in a join, then the result of this SELECT statement changes into a derived table and you've got to give this derived table a name so you can use the columns of the derived table.

    I hope it will be a little bit more clear now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置