没有反勾符号时,INSERT上出现MySQL错误

bq3bfh9z  于 2023-04-05  发布在  Mysql
关注(0)|答案(1)|浏览(119)

MySQL在这里。我试图让一个INSERT工作:

INSERT INTO users (
    user_ref_id,
    user_contact_info_id,
    user_secure_password,
    user_role_id,
    user_status_id
) VALUES (
    UUID(),
    SELECT ci.contact_info_id FROM contact_info ci WHERE ci.contact_info_email = 'admin@example.com',
    '12345',
    SELECT r.role_id FROM roles r WHERE r.role_label = 'ADMIN',
    SELECT us.user_status_id FROM user_statuses us WHERE us.user_status_label = 'ACTIVATED'
);

但我得到了:

SQL Error [1064] [42000]: 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
'SELECT ci.contact_info_id FROM contact_info ci WHERE ci.contact_info_email = 'ad' at line 9

当我用Google搜索MySQL 1064 42000错误时,它只是告诉我我的“反勾符号”有一个错误,但我的INSERT中没有这些错误。有人能发现我哪里出错了吗?

djmepvbi

djmepvbi1#

这应该行得通:

INSERT INTO users (
    user_ref_id,
    user_contact_info_id,
    user_secure_password,
    user_role_id,
    user_status_id
) VALUES (
    UUID(),
    (SELECT ci.contact_info_id FROM contact_info ci WHERE ci.contact_info_email = 'admin@example.com'),
    '12345',
    (SELECT r.role_id FROM roles r WHERE r.role_label = 'ADMIN'),
    (SELECT us.user_status_id FROM user_statuses us WHERE us.user_status_label = 'ACTIVATED')
);

每个SELECT from VALUES都应该包含在()中。

相关问题