Answers:
除了此答案之外,如果您需要根据tableA.value动态更改tableB.value,则可以执行以下操作:
UPDATE tableB
INNER JOIN tableA ON tableB.name = tableA.name
SET tableB.value = IF(tableA.value > 0, tableA.value, tableB.value)
WHERE tableA.name = 'Joe'
UPDATE participants_registrations INNER JOIN participants ON participants.id = participants_registrations.participantId INNER JOIN registrations ON registrations.id = participants_registrations.registrationId LEFT JOIN groups ON (groups.id = registrations.groupId) SET registrations.groupId = groups.id, registrations.groupName = groups.name, participants.memberOfGroupName = groups.name
您需要加入两个表:
例如,您要将name
tableA 的值复制到tableB
它们具有相同值的位置ID
UPDATE tableB t1
INNER JOIN tableA t2
ON t1.id = t2.id
SET t1.name = t2.name
WHERE t2.name = 'Joe'
更新1
UPDATE tableB t1
INNER JOIN tableA t2
ON t1.id = t2.id
SET t1.name = t2.name
更新2
UPDATE tableB t1
INNER JOIN tableA t2
ON t1.name = t2.name
SET t1.value = t2.value
where
条款或修改where
根据您的需要..节
第二种可能性是
UPDATE TableB
SET TableB.value = (
SELECT TableA.value
FROM TableA
WHERE TableA.name = TableB.name
);
.name
在两个表上都建立索引,这仍然会很慢吗?
将数据存储在临时表中
Select * into tempTable from table1
现在更新列
UPDATE table1
SET table1.FileName = (select FileName from tempTable where tempTable.id = table1.ID);
就我而言,公认的解决方案太慢了。对于具有18万行的表,更新速率约为每秒10行。这是与join元素上的索引一起使用的。
我终于使用以下过程解决了我的问题:
CREATE DEFINER=`my_procedure`@`%` PROCEDURE `rescue`()
BEGIN
declare str VARCHAR(255) default '';
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE cur_name VARCHAR(45) DEFAULT '';
DECLARE cur_value VARCHAR(10000) DEFAULT '';
SELECT COUNT(*) FROM tableA INTO n;
SET i=0;
WHILE i<n DO
SELECT namea,valuea FROM tableA limit i,1 INTO cur_name,cur_value;
UPDATE tableB SET nameb=cur_name where valueb=cur_value;
SET i = i + 1;
END WHILE;
END
我希望它能像我一样对以后的人有所帮助
如果两个表中都有公共字段,那就太简单了!....
表1 =您要在其中更新的表。表2 =您从中获取数据的表。
$qry_asseet_list = mysql_query("SELECT 'primary key field' FROM `table-1`");
$resultArray = array();
while ($row = mysql_fetch_array($qry_asseet_list)) {
$resultArray[] = $row;
}
foreach($resultArray as $rec) {
$a = $rec['primary key field'];
$cuttable_qry = mysql_query("SELECT * FROM `Table-2` WHERE `key field name` = $a");
$cuttable = mysql_fetch_assoc($cuttable_qry);
echo $x= $cuttable['Table-2 field']; echo " ! ";
echo $y= $cuttable['Table-2 field'];echo " ! ";
echo $z= $cuttable['Table-2 field'];echo " ! ";
$k = mysql_query("UPDATE `Table-1` SET `summary_style` = '$x', `summary_color` = '$y', `summary_customer` = '$z' WHERE `summary_laysheet_number` = $a;");
if ($k) {
echo "done";
} else {
echo mysql_error();
}
}
INNER JOIN
在这种情况下是完美的。我还曾经CONCAT_WS
合并过另一个表的产品名称和SKU