用另一个表的值更新mysql列

时间:2021-09-13 16:38:40

I have two tables, both looking like

我有两张桌子,看起来都像

id  name  value
===================
1   Joe     22
2   Derk    30

I need to copy the value of value from tableA to tableB based on check name in each table.

我需要根据每个表中的check name将值从表a复制到表b。

Any tips for this UPDATE statement?

对于这个更新语句有什么提示吗?

6 个解决方案

#1


266  

In addition to this answer if you need to change t1.value according to t2.value dynamically you can do for example:

除了这个答案,如果你需要改变t1。根据t2值。动态值你可以做,例如:

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'

#2


116  

you need to join the two tables:

你需要加入这两个表格:

for instance you want to copy the value of name from tableA into tableB where they have the same ID

例如,您希望将名称的值从表a复制到具有相同ID的表b

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.id = t2.id
SET t1.name = t2.name 
WHERE t2.name = 'Joe'

UPDATE 1

更新1

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.id = t2.id
SET t1.name = t2.name 

UPDATE 2

更新2

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.name = t2.name
SET t1.value = t2.value

#3


66  

Second possibility is,

第二个可能性是,

UPDATE TableB 
SET TableB.value = (
    SELECT TableA.value 
    FROM TableA
    WHERE TableA.name = TableB.name
);

#4


2  

The second option is feasible also if you're using safe updates mode (and you're getting an error indicating that you've tried to update a table without a WHERE that uses a KEY column), by adding:

如果您使用的是安全更新模式(并且您得到了一个错误,表明您试图在没有使用键列的地方更新表),那么第二个选项也是可行的。

UPDATE TableB  
SET TableB.value = (  
SELECT TableA.value  
    FROM TableA  
    WHERE TableA.name = TableB.name  
)  
**where TableB.id < X**  
;

#5


0  

--Store your data in temp table Select * into tempTable from table1

——将数据存储在temp表Select *中,并从表1中存储到tempTable中

--Now Update the column UPDATE table1 SET table1.FileName = (select FileName from tempTable where tempTable.id = table1.ID);

——现在更新列Update table1 SET table1。文件名=(在tempTable中选择文件名)。id = table1.ID);

#6


-2  

If you have common field in both table then it's so easy !....

如果你有相同的字段在表很简单! ....

Table-1 = table where you want to update. Table-2 = table where you from take data.

表1 =要更新的表。表2 =从表中获取数据。

  1. make query in Table-1 and find common field value.
  2. 在表1中进行查询,查找公共字段值。
  3. make a loop and find all data from Table-2 according to table 1 value.
  4. 做一个循环,根据表1的值从表2中查找所有数据。
  5. again make update query in table 1.
  6. 再次在表1中进行更新查询。

$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();
    }


}

#1


266  

In addition to this answer if you need to change t1.value according to t2.value dynamically you can do for example:

除了这个答案,如果你需要改变t1。根据t2值。动态值你可以做,例如:

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'

#2


116  

you need to join the two tables:

你需要加入这两个表格:

for instance you want to copy the value of name from tableA into tableB where they have the same ID

例如,您希望将名称的值从表a复制到具有相同ID的表b

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.id = t2.id
SET t1.name = t2.name 
WHERE t2.name = 'Joe'

UPDATE 1

更新1

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.id = t2.id
SET t1.name = t2.name 

UPDATE 2

更新2

UPDATE tableB t1 
        INNER JOIN tableA t2 
             ON t1.name = t2.name
SET t1.value = t2.value

#3


66  

Second possibility is,

第二个可能性是,

UPDATE TableB 
SET TableB.value = (
    SELECT TableA.value 
    FROM TableA
    WHERE TableA.name = TableB.name
);

#4


2  

The second option is feasible also if you're using safe updates mode (and you're getting an error indicating that you've tried to update a table without a WHERE that uses a KEY column), by adding:

如果您使用的是安全更新模式(并且您得到了一个错误,表明您试图在没有使用键列的地方更新表),那么第二个选项也是可行的。

UPDATE TableB  
SET TableB.value = (  
SELECT TableA.value  
    FROM TableA  
    WHERE TableA.name = TableB.name  
)  
**where TableB.id < X**  
;

#5


0  

--Store your data in temp table Select * into tempTable from table1

——将数据存储在temp表Select *中,并从表1中存储到tempTable中

--Now Update the column UPDATE table1 SET table1.FileName = (select FileName from tempTable where tempTable.id = table1.ID);

——现在更新列Update table1 SET table1。文件名=(在tempTable中选择文件名)。id = table1.ID);

#6


-2  

If you have common field in both table then it's so easy !....

如果你有相同的字段在表很简单! ....

Table-1 = table where you want to update. Table-2 = table where you from take data.

表1 =要更新的表。表2 =从表中获取数据。

  1. make query in Table-1 and find common field value.
  2. 在表1中进行查询,查找公共字段值。
  3. make a loop and find all data from Table-2 according to table 1 value.
  4. 做一个循环,根据表1的值从表2中查找所有数据。
  5. again make update query in table 1.
  6. 再次在表1中进行更新查询。

$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();
    }


}