将测试数据插入SQL表

时间:2013-07-14 22:19:25

标签: mysql sql

我试图将一些测试数据插入到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服务器版本对应的手册,以获得正确的语法,以便在'' 2914'附近使用。作为PostCode,'' AS CitySuburbName UNION ALL SELECT TimeSlotId FROM Time'在第4行

期望这样插入22个记录,其中每个单独的TimeSlotId来自现有记录,我试图避免硬编码其生成的ID。

  1. 首先,我哪里出错了?
  2. 有更好的方法吗?
  3. 这可以推断为每个子SELECT插入多个记录,DayCode通过MON,TUE ... SAT变化吗?

3 个答案:

答案 0 :(得分: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 演示

答案 1 :(得分:1)

0.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语句后粘贴它们。

0.2。我认为这是一个非常好的方式。最好的之一。 :)

0.3。我不确定你的意思,但听起来你可以将DayCode从where子句中删除,因此union中的每个查询都会返回多个记录。

答案 2 :(得分:1)

每个UNION-ed在一起的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)构造是不必要的。相反,您可以直接将UNIONed INSERT INSERT一起插入:

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

(依此类推)