我有一个类似的数据库:
table name = jos_school ... id =主键,name =唯一键
id name no_of_students no_of_staffs fees
1 schoolA 0 0 0
2 schoolB 0 0 0
...
...
在phpMyAdmin中,我做了类似这样的事情,并且工作正常(成功更新了多行和多列),
UPDATE jos_school
SET no_of_students = CASE name
WHEN 'schoolA' THEN '1523'
WHEN 'schoolB' THEN '546'
....
END,
no_of_staffs = CASE name
WHEN 'schoolA' THEN '1234'
WHEN 'schoolB' THEN '346'
....
END
WHERE name IN ('schoolA', 'schoolB')
但是,我无法使用JOOMLA的Update方法更新表。我不想使用foreach并将表更新超过1000次。我想执行一个查询。
我也读过:http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase#Updating_a_Record 和dint发现它在这种情况下很有帮助。 所以,有人能指出我正确的方向。
答案 0 :(得分:1)
您可以查询如下,
// Create a new query object.
$db = &JFactory::getDBO();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query="UPDATE jos_school SET no_of_students = CASE name";
foreach($data as $d)
{
$query.=".....";
}
$query.=".....";
$db->setQuery((string)$query);
希望有帮助...