【问题标题】:What is wrong with this query, multiple inserts这个查询有什么问题,多次插入
【发布时间】:2012-09-26 18:38:41
【问题描述】:

这个 MySQL 查询有什么问题,我该如何解决?

SELECT f.uniqueidentifier FROM db.engine4_passport_userforms uf
INNER JOIN db.engine4_passport_forms f ON uf.form_id = f.form_id
WHERE uf.user_id = 10
INSERT INTO db.engine4_passport_registrationchildren (parentid, `First Name`, Surname, Relationship) VALUES ( f.uniqueidentifier, '', '', '')
INSERT INTO db.engine4_passport_registrationcontactdetails (Address, Telephone, UID, Postcode) VALUES ('3 street', '',  f.uniqueidentifier, 'aaaaaa')
INSERT INTO db.engine4_passport_registrationcontactprefs (parentid, prefs) VALUES ( f.uniqueidentifier, 'Post Email ')
INSERT INTO db.engine4_passport_registrationfoundus (parentid, medium, `reason for joining`) VALUES ( f.uniqueidentifier, '', '')
INSERT INTO db.engine4_passport_registrationhousehold (parentid, `chief earner occupation`, `number of people`, `number of children`) VALUES ( f.uniqueidentifier, 'build', '1', '0')
INSERT INTO db.engine4_passport_registrationinterests (parentid, interest) VALUES ( f.uniqueidentifier, '')
INSERT INTO db.engine4_passport_registrationpersonaldetails (parentid, ethnicity) VALUES ( f.uniqueidentifier, 'White')
INSERT INTO db.engine4_passport_registrationqanda VALUES ( f.uniqueidentifier, '4', '1', '1', '1', '3', '3', '5', '5', '3', '4', '5', '4')
INSERT INTO db.engine4_passport_registrationteam (parentid, team?, `no of team members`, relationship) VALUES ( f.uniqueidentifier, 'No', '', '')
INSERT INTO db.engine4_passport_registrationuserlink (userid, regformUID) VALUES ('10',  f.uniqueidentifier);

这是我得到的错误:

Script line: 1  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO db.engine4_passport_registrationchildren (parentid, `Fir' at line 2

【问题讨论】:

    标签: mysql select error-handling insert


    【解决方案1】:

    这些是多个查询,而不仅仅是一个。在两者之间添加; 以分隔它们:

    select ....
    inner join ...
    where ...;
    
    insert into ...;
    values ...
    
    ...
    

    编辑

    如果您想使用先前选择的值,则可以使用变量。示例:

    SELECT @var := f.uniqueidentifier 
    FROM db.engine4_passport_userforms uf
    INNER JOIN db.engine4_passport_forms f ON uf.form_id = f.form_id
    WHERE uf.user_id = 10;
    
    INSERT INTO db.engine4_passport_registrationchildren (parentid, `First Name`, Surname, Relationship) 
    VALUES (@var, '', '', '');
    

    【讨论】:

    • 如果我这样做,它会说unknown coloumn f.uniqueindentifier in field list
    • 那么你的表中没有uniqueindentifier这样的列。
    • 这是因为如果我运行 SELECT f.uniqueidentifier FROM engine4_passport_userforms uf INNER JOIN db.engine4_passport_forms f ON uf.form_id = f.form_id WHERE user_id = 10; 它会返回我想要的内容
    • 不要在selectinner join 之间放置;
    【解决方案2】:

    多个查询必须以分号; 结束。没有分号的单个查询是可以的,但是如果您向 mysql 服务器发送多个查询,它们必须由 ; 终止

    【讨论】:

    • 如果我这样做,它会说unknown coloumn f.uniqueindentifier in field list
    猜你喜欢
    • 2011-09-29
    • 2011-10-13
    • 2021-09-02
    • 2010-09-07
    • 2010-10-04
    • 2011-01-15
    • 2021-05-08
    相关资源
    最近更新 更多