[英]sql select then update results where
我正在尝试更新Qq列,其中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';
?>
您应该使用第二个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效率低下,或另一个瓶颈:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.