我正在尝试将一些测试数据插入到 MySQL 数据库中,该数据库混合了参考 ID 和变量值。我的声明被 MySQL 拒绝,因为它是通用的(并且显然是毫无价值的)“xxx 附近的语法错误”错误是:
INSERT INTO TimeSlotWorker (TimeSlotId, WorkerId, PostCode, CitySuburbName)
SELECT TimeSlotId, 1 , PostCode, CitySuburbName
FROM (
SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 1), '2914' AS PostCode, '' AS CitySuburbName
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 2), '2912' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 2), '2913' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 2), '2911' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Charnwood'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Dunlop'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Florey'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Flynn'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Fraser'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Higgins'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Holt'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Kippax'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Latham'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Macgregor'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Melba'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2615' , 'Spence'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2614' , 'Aranda'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 3), '2614' , 'Cook'
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 4), '2617' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 5), '2602' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 5), '2612' , ''
UNION ALL SELECT TimeSlotId FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 6), '2609' , ''
)
这使:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的 ''2914' AS PostCode, '' AS CitySuburbName UNION ALL SELECT TimeSlotId FROM Time' 附近使用正确的语法
期望这将插入 22 条记录,其中每条记录的奇异 TimeSlotId 来自现有记录,我试图避免对其生成的 ID 进行硬编码。
- 首先,我哪里出错了?
- 有没有更好的办法?
- 这是否可以推断为每个子 SELECT 插入多个记录,其中 DayCode 在 MON、TUE ... SAT 中变化?