SQL:我可以从其他表中的变量中插入某些字段吗? [英] SQL: Can I INSERT some fields from variables other from another table

查看:68
本文介绍了SQL:我可以从其他表中的变量中插入某些字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL问题

我想知道是否可以编写一条sql INSERT语句在表中插入一行,其中某些字段来自变量,而其他字段则来自另一个表.目前,我从tableA中进行选择,将字段存储在变量中,然后将其插入到tableB中. 在下面的示例中,我想从tableA获取field1field2field3,其余部分从变量获取. (mysql)

I wonder if I can write a single sql INSERT statement to insert a row into a table where some of the fields come from variables and others come from another table. Currently I select from tableA, store the fields in variables and then insert them into tableB. In the following example I'd like to get field1, field2, and field3 from tableA and the rest from variables. (mysql)

$sql = "SELECT field1, field2, field3 FROM tableA where product = 5";

php代码

$sql = "INSERT tableB
SET     
fkey = 100,
custid = 10,
product = 5,
field1 = '$field1',
field2 = '$field2',
field3 = '$field3' ";

推荐答案

是的,您只需在选择中插入文字值和字段并插入:

Yes, you just mix literal values and fields in a select and insert:

insert into tableB (fkey, custid, product, field1, field2, field3)
select 100, 10, 5, field1, field2, field3
from tableA
where product = 5

这篇关于SQL:我可以从其他表中的变量中插入某些字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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