0

我有以下插入语句:

$sql ="INSERT INTO `firm`(name, VAT, active) VALUES ('$name', '$VAT', '$active')"; 

$sql = "INSERT INTO `area`(name, hub_name, fk_hub_id) VALUES ('$areaname',(SELECT `name` from hub WHERE name = '$hub_name'), (SELECT `id` from hub WHERE name = '$hub_name'))";

$sql ="INSERT INTO 'contactdetails'
        (fk_firm_id,
         address_physical_line_1,
         address_physical_line_2,
         address_physical_line_3,
         address_physical_line_4,
         address_physical_line_5,
         address_physical_line_6,
         address_physical_line_7,
         address_physical_code,
         address_postal_line_1,
         address_postal_line_2,
         address_postal_line_3,
         address_postal_line_4,
         address_postal_line_5,
         address_postal_line_6,
         address_postal_line_7,
         address_postal_code,
         fax_1,
         fax_2,
         phone_1,
         phone_2,
         phone_3,
         phone_4)
VALUES      ( (SELECT `id`
           FROM   firm
           WHERE  name = '$name'),
          '$address_physical_line_1',
          '$address_physical_line_2',
          '$address_physical_line_3',
          '$address_physical_line_4',
          '$address_physical_line_5',
          '$address_physical_line_6',
          '$address_physical_line_7',
          '$address_physical_code',
          '$address_postal_line_1',
          '$address_postal_line_2',
          '$address_postal_line_3',
          '$address_postal_line_4',
          '$address_postal_line_5',
          '$address_postal_line_6',
          '$address_postal_line_7',
          '$address_postal_code',
          '$fax_1',
          '$fax_2',
          '$phone_1',
          '$phone_2',
          '$phone_3',
          '$phone_4')  ";

我是否必须使用事务语句来运行这三个查询。我从未使用过事务性报表。一个语句取决于其他语句的值。

4

2 回答 2

0

MySQLAUTO_COMMIT默认设置为 true。这意味着脚本中的每个查询都将在紧随其后的查询之前执行。

这使您可以执行以下操作:

// Here I admit that the table is empty, with an auto-incremented id.
INSERT INTO test VALUES ('', 'First');
INSERT INTO test ('', SELECT value FROM test WHERE id = "1");

在这里,您将插入第一行,id=1, value="First"然后id=2, value="First"

于 2013-08-06T13:02:00.990 回答
0

我不确定是否真正理解您的问题,但如果需要执行多个 SQL 请求以确保它们全部完成或未完成,您必须显式创建并提交事务:

START TRANSACTION
INSERT ...
INSERT ...
INSERT ...

-- All is ready, apply "all at once"
COMMIT

http://dev.mysql.com/doc/refman/5.0/en/commit.html


为了清楚(?),从您的事务内部,所有 SQL 语句似乎都像往常一样“一个接一个地”执行。但是从外部世界(其他事务/与您的 SQL 服务器的连接)在您COMMIT的事务之前不会出现任何更改 - 然后所有更改将“一次全部”出现。

于 2013-08-06T13:54:00.790 回答