Is there a way I can use a combination of hard values and a subquery to insert into a table with one command?
有没有办法可以使用硬值和子查询的组合来插入一个命令的表?
For example:
例如:
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name
FROM customers
WHERE city = 'San Diego';
I need supplier_type to be 3. So can I do the following for the second line?
我需要supplier_type为3.所以我可以为第二行执行以下操作吗?
SELECT account_no, name, supplier_type = 3
supplier_type is not in the customers table
supplier_type不在customers表中
2 个解决方案
#1
14
Just add it with your SELECT
fields.
只需将其与SELECT字段一起添加即可。
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name, 3 AS supplier_type
FROM customers
WHERE city = 'San Diego';
#2
1
Even simpler, just fill in the field with the value, dont even need an AS:
更简单,只需用值填写字段,甚至不需要AS:
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name, 3
FROM customers
WHERE city = 'San Diego';
#1
14
Just add it with your SELECT
fields.
只需将其与SELECT字段一起添加即可。
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name, 3 AS supplier_type
FROM customers
WHERE city = 'San Diego';
#2
1
Even simpler, just fill in the field with the value, dont even need an AS:
更简单,只需用值填写字段,甚至不需要AS:
INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name, 3
FROM customers
WHERE city = 'San Diego';