dongxie3963 2016-08-11 03:36
浏览 15
已采纳

too long

I'm having a problem. I have a table in which I want to move its data into another table. When I do : INSERT INTO rosters SELECT * FROM rosters_test
All of the rows enter into the database. The problem is that the columns are out of order for the new data entered (the new data has id where name should be etc), therefore making the data incorrect and useless.
ohcrap I have tried to be more specific about the insert statement, but that is where the ERROR: UNIQUE constraint failed occurs. I have a dual key on the rosters table which is the id and the season. I am trying to insert the 20142015 roster from the rosters_test table into the rosters table where the 20152016 and 20162017 rosters reside.

Every row in the roster_test table is 20142015 for the season and I checked all 27 rows for repeat ids and there are none. These are the queries I tried:

INSERT INTO 'rosters'
SELECT 'number' AS 'number',  'name' AS 'name' , 'height' AS 'height', 'weight' AS 'weight', 'birthplace' AS 'birthplace', 'birthdate' AS 'birthdate', 'position' AS 'position', 'id' AS 'id', 'age' AS 'age', 'season' AS 'season', 'imageURL' AS 'imageURL'
FROM 'rosters_test' 

INSERT INTO 'rosters'  (number, name, height, weight, birthplace, birthdate ,position ,id ,age ,season, imageURL) 
SELECT 'number',  'name' , 'height', 'weight', 'birthplace', 'birthdate', 'position', 'id', 'age' , 'season', 'imageURL'
FROM 'rosters_test'

I also tried things such as rosters_test.number AS rosters.number as well. Nothing is working: Here is the schema of the two tables:

CREATE TABLE 'rosters_test' ('number' INTEGER, 'name' TEXT, 'height' INTEGER, 'weight' INTEGER, 'birthplace' TEXT, 'birthdate' TEXT, 'position' TEXT, 'id' INTEGER, 'age' INTEGER, 'season' INTEGER, 'imageURL' TEXT)

I had a dual key on the rosters_test table at one point too.

CREATE TABLE 'rosters' ('position' TEXT, 'id' INTEGER, 'weight' INTEGER, 'height' TEXT, 'imageURL' TEXT, 'birthplace' TEXT, 'age' INTEGER, 'name' TEXT, 'birthdate' TEXT, 'number' INTEGER, 'season' INTEGER, PRIMARY KEY('id','season'))
  • 写回答

1条回答 默认 最新

  • dongquanlin1885 2016-08-11 03:49
    关注

    Use INSERT INTO...SELECT and explicity specify the columns (and their order) which you want for the rosters table:

    INSERT INTO rosters (position, id, weight, height, imageURL, birthplace, age, name,
                         birthdate, number, season)
    SELECT position, id, weight, height, imageURL, birthplace, age, name, birthdate,
           number, season
    FROM rosters_test
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c