所以我看了一下HERE,但是对于我正在做的事情的简单性似乎有点复杂。最多我会处理一百个要更新的项目(并且大部分时间)它会变得更像40)
目前我有类似的东西
$sql_update = '';
for($x = 0; $x < count($nodes); $x++){
if($nodes[$x]['loaded'] == 'true'){
if($nodes[$x]['changed'] == 'true'){
$sql_update .= 'UPDATE `genetic_decomp`.`tbl_node2view` SET `x` = "'.$nodes[$x]['location']['x'].'", `y` = "'.$nodes[$x]['location']['y'].'" WHERE `tbl_node2view`.`id` = "'.$nodes[$x]['id'].'";';
$sql_update .= 'UPDATE `genetic_decomp`.`tbl_nodes` SET `name` = "'.$nodes[$x]['name'].'", `type` = "'.$nodes[$x]['type'].'" WHERE `tbl_nodes`.`node_id` = "'.$nodes[$x]['id'].'";';
}
}
}
if($sql_update != ''){
$sql_result=mysql_query($sql_update,$connection) or exit("Sql Error".mysql_error());
}
现在当我得到它只打印echo $sql_update
输出然后将输出粘贴到MAMP中的SQL框中它工作正常..通过并更新我想要的两个表中的行< / p>
然而,当我运行上面的代码时,它吐了回来:
Sql Error
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE `genetic_decomp`.`tbl_nodes` SET `name` = "lala", `type` = "p" WHERE `tbl' at line 1
我在做错了什么?
有更好的方法吗?
答案 0 :(得分:4)
你的SQL在语法上看起来是正确的(除非我错过了一些简单的东西)。实际问题是因为您正在使用mysql_query()
- 不支持多个语句;因此,您无法使用此方法在一个中运行两个UPDATE
个查询。
从手册:
mysql_query()发送一个唯一的查询(多个查询不是 支持)
在同一个说明中,mysql_
方法已被弃用,因此我(和社区)会建议您更新代码以使用mysqli_
或PDO
方法 - 这两种方法都支持单个语句中的多个查询。
如果您需要坚持mysql_query()
(而不是重构整个应用程序),只需拆分查询并背靠背运行。
答案 1 :(得分:0)
如果您使用的是mysql_query,则需要单独更新它们。
答案 2 :(得分:0)
作为mysqli的替代品,我发现了这个: http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/
想法
UPDATE categories
SET display_order = CASE id
WHEN 1 THEN 3
WHEN 2 THEN 4
WHEN 3 THEN 5
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
现实生活中的例子
// An array containing the category ids as keys and the new positions as values
$display_order = array(
1 => 4,
2 => 1,
3 => 2,
4 => 3,
5 => 9,
6 => 5,
7 => 8,
8 => 9
);
$ids = implode(',', array_keys($display_order));
$sql = "UPDATE categories SET display_order = CASE id ";
foreach ($display_order as $id => $ordinal) {
$sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal);
}
$sql .= "END WHERE id IN ($ids)";
echo $sql;
我的案例
我收到的是数组
Array
(
[0] => stdClass Object
(
[movement_id] => 278
[leg_miles] => 3.5
[leg_km] => 5.63
)
[1] => stdClass Object
(
[movement_id] => 279
[leg_miles] =>
[leg_km] =>
)
[2] => stdClass Object
(
[movement_id] => 280
[leg_miles] =>
[leg_km] =>
)
)
我的功能(必须使用sprintf上的浮动)
//get ids string
$ids = "";
foreach ($movementsData as $movement) {
$ids .= $movement->movement_id.",";
}
$ids = rtrim($ids, ",");
//MySQL query construct
$sql = "UPDATE movements SET movement_miles = CASE movement_id ";
foreach ($movementsData as $movement) {
$sql .= sprintf("WHEN %d THEN %.2f ", $movement->movement_id, $movement->leg_miles);
}
$sql .= "END, ";
$sql .= "movement_km = CASE movement_id ";
foreach ($movementsData as $movement) {
$sql .= sprintf("WHEN %d THEN %.2f ", $movement->movement_id, $movement->leg_km);
}
$sql .= "END ";
$sql .= "WHERE movement_id IN (".$ids.")";
最终查询结果
UPDATE movements SET
movement_miles = CASE movement_id
WHEN 278 THEN 3.50
WHEN 279 THEN 0.00
WHEN 280 THEN 0.00
END,
movement_km = CASE movement_id
WHEN 278 THEN 5.63
WHEN 279 THEN 0.00
WHEN 280 THEN 0.00
END
WHERE movement_id IN (278,279,280)
答案 3 :(得分:0)