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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)