如果我想同时在两个表中插入一些数据,这是可能的吗?但在table2中,我只是插入选定项,而不像table1那样插入所有数据。下面是单独的查询:
$sql = "INSERT INTO table1(model, serial, date, time, qty) VALUES ('star', '0001', '2010-08-23', '13:49:02', '10')";
$sql2 = "INSERT INTO table2(model, date, qty) VALUES ('star', '2010-008-23', '10')"; 我可以在table2上插入COUNT(model)吗?我找到了一些脚本,我可以用这个吗?
$sql = "INSERT INTO table1(model, serial, date, time, qty) VALUES ('star', '0001', '2010-08-23', '13:49:02', '10')";
$result = mysql_query($sql,$conn);
if(isset($model))
{
$model = mysql_insert_id($conn);
$sql2 = "INSERT INTO table2(model, date, qty) VALUES ('star', '2010-008-23', '10')";
$result = mysql_query($sql,$conn);
}
mysql_free_result($result);发布于 2010-08-23 15:31:17
答案很简单,没有办法在一个命令中将数据插入到两个表中。我很确定你的第二个脚本不是你想要的。
通常,这样的问题可以根据您的实际需求通过以下方法之一来解决:
希望这能有所帮助
发布于 2011-04-06 22:39:47
//如果要插入与第一个表相同的表
$qry = "INSERT INTO table (one, two, three) VALUES('$one','$two','$three')";
$result = @mysql_query($qry);
$qry2 = "INSERT INTO table2 (one,two, three) VVALUES('$one','$two','$three')";
$result = @mysql_query($qry2);//或者如果您想插入表1的某些部分
$qry = "INSERT INTO table (one, two, three) VALUES('$one','$two','$three')";
$result = @mysql_query($qry);
$qry2 = "INSERT INTO table2 (two) VALUES('$two')";
$result = @mysql_query($qry2);//我知道它看起来太好了,但它是有效的,您可以继续添加查询,只需更改
"$qry"-number and number in @mysql_query($qry"")发布于 2010-08-23 15:08:44
它不能在一个语句中完成,
如果表是由innodb engine创建的,则可以使用transaction确保将数据插入到两个表中
https://stackoverflow.com/questions/3545312
复制相似问题