插入select mysql

时间:2017-10-21 00:52:07

标签: mysql

MySQL新人在这里。我已经构建了一个包含环境变量的数据库。每个值都与唯一的classdesignationvariable相关联,每个值都有一个定义表。当我添加一个新变量时,我需要将每个定义表中的外键复制到一个新表variable_mappings中,并添加一个值。 variable_mappings表如下所示:

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| value          | varchar(512) | NO   |     | NULL    |                |
| designation_id | int(11)      | NO   | MUL | NULL    |                |
| class_id       | int(11)      | NO   | MUL | NULL    |                |
| variable_id    | int(11)      | NO   | MUL | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

现在添加变量的命令看起来像这样:

INSERT INTO variable_mappings (variable_id, designation_id, class_id, value)
    SELECT variable_definitions.id as 'variable', 
           designation_definitions.id as 'designation', 
           class_definitions.id as 'class' 
    FROM variable_definitions 
    JOIN designation_definitions on designation_definitions.name='development' 
    JOIN class_definitions on class_definitions.name='av-control' 
    WHERE variable_definitions.name='CONFIGURATION-DATABASE-MICROSERVICE-ADDRESS';

MySQL抱怨value没有默认值。向variable_mappings添加行的最佳方式是什么?

2 个答案:

答案 0 :(得分:0)

尝试从INSERT语句列列表中删除值:

INSERT INTO variable_mappings (variable_id, designation_id, class_id)
SELECT variable_definitions.id as 'variable', 
       designation_definitions.id as 'designation', 
       class_definitions.id as 'class' 
FROM variable_definitions 
JOIN designation_definitions on designation_definitions.name='development' 
JOIN class_definitions on class_definitions.name='av-control' 
WHERE variable_definitions.name='CONFIGURATION-DATABASE-MICROSERVICE-ADDRESS';

或在SELECT语句中添加一个字段

INSERT INTO variable_mappings (variable_id, designation_id, class_id, value)
SELECT variable_definitions.id as 'variable', 
       designation_definitions.id as 'designation', 
       class_definitions.id as 'class',
       blablabla as 'value'
FROM variable_definitions 
JOIN designation_definitions on designation_definitions.name='development' 
JOIN class_definitions on class_definitions.name='av-control' 
WHERE variable_definitions.name='CONFIGURATION-DATABASE-MICROSERVICE-ADDRESS';

答案 1 :(得分:0)

INSERT INTO SELECT语句要求SELECT语句返回与括号中的列数相同的列数。在您的情况下,您要求MySQL插入4列,但您的SELECT语句只返回3列。快速解决此问题的方法是在NULL中添加SELECT,如下所示:

INSERT INTO variable_mappings (variable_id, designation_id, class_id, value)
SELECT variable_definitions.id as 'variable', 
       designation_definitions.id as 'designation', 
       class_definitions.id as 'class',
       NULL /* default value for the 'value' column */
FROM variable_definitions 
JOIN designation_definitions on designation_definitions.name='development' 
JOIN class_definitions on class_definitions.name='av-control' 
WHERE variable_definitions.name='CONFIGURATION-DATABASE-MICROSERVICE-ADDRESS';

另一种方法是简单地从要插入的列列表中删除value。由于该列在DDL中的默认值为NULL,因此该值将设置为。

INSERT INTO variable_mappings (variable_id, designation_id, class_id)
SELECT variable_definitions.id as 'variable', 
       designation_definitions.id as 'designation', 
       class_definitions.id as 'class' 
FROM variable_definitions 
JOIN designation_definitions on designation_definitions.name='development' 
JOIN class_definitions on class_definitions.name='av-control' 
WHERE variable_definitions.name='CONFIGURATION-DATABASE-MICROSERVICE-ADDRESS';