You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

MySQL 1064语法错误求助:INSERT嵌套SELECT查询失败

Fixing Your MySQL INSERT Syntax Error

Hey there! I see exactly what's causing that #1064 syntax error in your INSERT statement. The issue is with how you're structuring the subquery to pull the M_ID from the matieres table.

What Went Wrong

Your current code wraps only the column and table part in parentheses instead of the entire subquery. MySQL expects a scalar subquery (one that returns a single value) to be fully enclosed in parentheses when used as a value in the VALUES clause.

Corrected INSERT Statement

Here's the fixed version of your query:

INSERT INTO utilisateurs (U_ID, NAME, SURNAME, LOGIN, PASSWORD, TYPE, M_ID)
VALUES ('4', 't', 't', 't', 't', 'Formateur', (SELECT M_ID FROM matieres WHERE LABEL = 'Anglais'));

Key Notes to Avoid Future Issues

  • Always wrap the entire subquery in parentheses when using it as a value in VALUES.
  • Make sure the subquery returns exactly one row/value. If there are multiple matieres records with LABEL = 'Anglais', this will throw another error. You can add LIMIT 1 to guarantee a single result if needed:
    (SELECT M_ID FROM matieres WHERE LABEL = 'Anglais' LIMIT 1)
    
  • Alternatively, you can rewrite this using an INSERT ... SELECT syntax which might be cleaner for this scenario:
    INSERT INTO utilisateurs (U_ID, NAME, SURNAME, LOGIN, PASSWORD, TYPE, M_ID)
    SELECT '4', 't', 't', 't', 't', 'Formateur', M_ID
    FROM matieres
    WHERE LABEL = 'Anglais';
    

内容的提问来源于stack exchange,提问作者DevJ

火山引擎 最新活动