带有子查询和值的 SQL INSERT INTO [英] SQL INSERT INTO with subquery and value

查看:50
本文介绍了带有子查询和值的 SQL INSERT INTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用硬值和子查询的组合通过一个命令插入到表中?

Is there a way I can use a combination of hard values and a subquery to insert into a table with one command?

例如:

INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name
FROM customers
WHERE city = 'San Diego';

我需要供应商类型为 3.那么我可以为第二行做以下操作吗?

I need supplier_type to be 3. So can I do the following for the second line?

SELECT account_no, name, supplier_type = 3

supplier_type 不在客户表中

推荐答案

只需将其添加到您的 SELECT 字段中即可.

Just add it with your SELECT fields.

INSERT INTO suppliers (supplier_id, supplier_name, supplier_type)
SELECT account_no, name, 3 AS supplier_type
FROM customers
WHERE city = 'San Diego';

这篇关于带有子查询和值的 SQL INSERT INTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆