我正在尝试更新数量列,其中suppliers_stock_code ='" 。 $ itemno。 "但products_model不包含' - '
我试过了
$sql = "UPDATE products set suppliers_qty = '" . $qty . "' (SELECT suppliers_stock_code, products_model from products where products_model NOT LIKE '%--%' and products_status = '1' and suppliers_stock_code = '" . $itemno . "')";
但这似乎没有插入值,任何人都可以看到这条线有什么问题吗?
我也试过
$sql = "update products set suppliers_qty= '" . $qty . "' where " . PRODUCTS_MODEL . " = '" . $selected['products_model'] . "' and " . SUPPLIERS_MODEL . "= '" . $itemno . "' and " . PRODUCTS_MODEL . " NOT LIKE '%--%'";
这可行,但需要很长时间才能执行,因此希望预先选择要更新的项目。
继承完整的脚本可能有更好(更快)的方式吗?
<?php
$working_dir = '../feeds/csv';
$local_file = 'test.csv';
$type_sep = ",";
$item_pos = 2;
$qty_pos = 3;
$item_pos -= 1;
$qty_pos -= 1;
chdir($working_dir);
$handle = fopen($local_file, 'w');
require('includes/configure.php');
require('includes/functions/database.php');
tep_db_connect_script() or die('Unable to connect to database server!');
$lines = file($local_file);
foreach ($lines as $line) {
$items = explode ($type_sep, $line);
$itemno = $items[$item_pos];
$qty = $items[$qty_pos] * 0.10;
$sql = "UPDATE products set suppliers_qty = '" . $qty . "'
WHERE products_model NOT LIKE '%--%'
AND products_status = '1' AND suppliers_stock_code = '" . $itemno . "'";
$result = mysql_query($sql);
}
}
tep_db_close_script();
echo 'Finished and closed database connection';
?>
答案 0 :(得分:0)
您应该使用第二个SQL:
$sql = "update products set suppliers_qty= '" . $qty . "' where " . PRODUCTS_MODEL . " = '" . $selected['products_model'] . "' and " . SUPPLIERS_MODEL . "= '" . $itemno . "' and " . PRODUCTS_MODEL . " NOT LIKE '%--%'";
第一个不起作用,因为更新将在预先选择的结果上执行,而不是在“真实”表上执行(顺便说一句:在你的1.sql中你必须选择suppliers_qty
列,如果你想要更新它(即使没有体力劳动))。
当sql需要很长时间时,可能是因为你有很多数据(或者是一个效率低下的DBMS,或者是另一个瓶颈:))