0

我正在尝试将一些测试数据插入到 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 进行硬编码。

  1. 首先,我哪里出错了?
  2. 有没有更好的办法?
  3. 这是否可以推断为每个子 SELECT 插入多个记录,其中 DayCode 在 MON、TUE ... SAT 中变化?
4

3 回答 3

2

要使您当前的代码正常工作,请将其更改为

INSERT INTO TimeSlotWorker (TimeSlotId, WorkerId, PostCode, CitySuburbName)
SELECT                      TimeSlotId, 1       , PostCode, CitySuburbName
FROM 
(
            SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 1) TimeSlotId, '2914' PostCode, '' CitySuburbName
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2)           , '2912'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2)           , '2913'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2)           , '2911'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Charnwood'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Dunlop'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Florey'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Flynn'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Fraser'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Higgins'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Holt'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Kippax'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Latham'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Macgregor'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Melba'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2615'         , 'Spence'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2614'         , 'Aranda'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3)           , '2614'         , 'Cook'
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 4)           , '2617'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 5)           , '2602'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 5)           , '2612'         , ''
  UNION ALL SELECT (SELECT TimeSlotId FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 6)           , '2609'         , ''
) q

这是SQLFiddle演示

你可以这样重写

INSERT INTO TimeSlotWorker (TimeSlotId, WorkerId, PostCode, CitySuburbName)
SELECT TimeSlotId, 1, '2914', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 1 UNION ALL 
SELECT TimeSlotId, 1, '2912', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2 UNION ALL
SELECT TimeSlotId, 1, '2913', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2 UNION ALL
SELECT TimeSlotId, 1, '2911', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 2 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Charnwood' FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Dunlop'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Florey'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Flynn'     FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Fraser'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Higgins'   FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Holt'      FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Kippax'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Latham'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Macgregor' FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Melba'     FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2615', 'Spence'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2614', 'Aranda'    FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2614', 'Cook'      FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 3 UNION ALL
SELECT TimeSlotId, 1, '2617', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 4 UNION ALL
SELECT TimeSlotId, 1, '2602', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 5 UNION ALL
SELECT TimeSlotId, 1, '2612', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 5 UNION ALL
SELECT TimeSlotId, 1, '2609', ''          FROM TimeSlot WHERE DayCode = 'MON' AND SequenceNbr = 6

这是SQLFiddle演示

于 2013-07-15T02:26:36.753 回答
1

每一个 UNION-ed together SELECT 语句都有相同的语法错误,无法解析。孤立地考虑第一个

SELECT TimeSlotId FROM TimeSlot 
   WHERE (DayCode = 'MON' AND SequenceNbr = 1), 
  '2914' AS PostCode, '' AS CitySuburbName

这些额外的列名必须都在 FROM 关键字之前:

SELECT TimeSlotId, '2914' AS PostCode, '' AS CitySuburbName
   FROM TimeSlot 
   WHERE (DayCode = 'MON' AND SequenceNbr = 1), 

另外,整个 SELECT FROM (SELECT) 构造是不必要的相反,您可以直接 INSERT UNIONed together SELECT 语句:

 INSERT INTO Table (ColList)
    SELECT SameNumberOfColumns FROM OtherTable WHERE . . .
    UNION ALL
    SELECT SameNumberOfColumns FROM OtherTable WHERE . . .

(等等)

于 2013-07-15T02:43:17.210 回答
1

.1。您的子查询只有一列 TimeslotId,而在主查询中,您选择了三列(PostCode 和 CitySuburbName 也是如此)。这不是真正的语法错误,但它是不正确的。语法错误在于您确实在 select 语句之后添加了那些缺少的字段,而它们应该是其中的一部分。

您需要像这样格式化子查询:

          SELECT TimeSlotId, '2914' AS PostCode, '' AS CitySuburbName FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 1)
UNION ALL SELECT TimeSlotId, '2912', '' FROM TimeSlot WHERE (DayCode = 'MON' AND SequenceNbr = 2)
UNION ALL ...

因此,将 PostCode 和 CitySuburbName 的常量值作为字段列表的一部分。您不能只在 select 语句之后粘贴它们。

.2. 我认为这是一个非常好的方法。最好的之一。:)

.3. 我不确定你的意思,但听起来你可以DayCode省略 where 子句,所以联合中的每个查询都会返回多条记录。

于 2013-07-14T22:24:25.523 回答