SQL问题
我想知道是否可以编写单个sqlINSERT语句在表中插入一行,其中某些字段来自变量,而其他字段则来自另一个表。目前,我从中进行选择tableA,将字段存储在变量中,然后将其插入tableB。在下面的例子中,我想获得field1,field2以及field3从tableA和变量休息。(mysql)
INSERT
tableA
tableB
field1
field2
field3
$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' ";
是的,您只需在选择中插入文字值和字段并插入:
insert into tableB (fkey, custid, product, field1, field2, field3) select 100, 10, 5, field1, field2, field3 from tableA where product = 5