更新MySQL数据库中两个不同表的值

时间:2017-09-01 08:54:33

标签: mysql database sql-update

这是MySQL数据库中的 table_start

+--------+---------+-----------+
| userID | userTfc | userCount |
+--------+---------+-----------+
| 11655  | SN10    |        45 |
| 11655  | SN16    |        80 |
| 11655  | SN24    |       796 |
| 11655  | SN35    |        56 |
+--------+---------+-----------+

我需要在此模式下更新同一MySQL数据库中的 table_end

+--------+------------+------------------+------------+------------------+------------+------------------+------------+------------------+
| userID | userTfc_01 | userTfc_01_Count | userTfc_02 | userTfc_02_Count | userTfc_03 | userTfc_03_Count | userTfc_04 | userTfc_04_Count |
+--------+------------+------------------+------------+------------------+------------+------------------+------------+------------------+
| 11655  | SN10       | 45               | SN16       | 80               | SN24       | 796              | SN35       | 56               |
+--------+------------+------------------+------------+------------------+------------+------------------+------------+------------------+

对于相同的 userID ,我需要更新:

  1. table_start userTfc 第一个 table_end userTfc_01 ;
  2. table_start userCount 第一个 table_end userTfc_01_Count ;
  3. table_start 列的第二 userTfc table_end userTfc_02 列;
  4. table_start userCount 第二个 table_end userTfc_02_Count ;
  5. table_start userTfc 第三个​​值到table_end的 userTfc_03 列;
  6. table_start 列的第三个​​ userCount table_end userTfc_03_Count 列;
  7. table_start 列的第四 userTfc table_end userTfc_04 列;
  8. table_start userCount 第四个 table_end userTfc_04_Count ;
  9. 你能帮助我吗?

    提前感谢您的帮助,非常感谢。

2 个答案:

答案 0 :(得分:1)

首先为您的第一个表执行此查询

$query1 = "SELECT userTfc,userCount FROM  table_start WHERE userID='11655' ";

我假设在上面的查询后你得到一个数组的结果让我们说$ result。现在,您可以使用循环更新第二个表。例如:

if(!empty($result)){ // check if array is not empty
for ($i=1; $i <= sizeof($result); $i++) { 
    $query2 = "UPDATE  table_end  SET userTfc_0'".$i."' = '".$result[$i]['userTfc']."' WHERE userID='11655' ";
    mysql_query($query2);
}
}

答案 1 :(得分:0)

您可以尝试以下查询。使用mysql的order by,limit和offset。按查询排序然后使用limit获取一条记录并使用偏移量来选择行号。

update table_end te 
set userTfc_01 = (select userTfc from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 0),
set userTfc_01_Count = (select userCount  from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 0),

set userTfc_02 = (select userTfc from table_start ts where ts.userid = te.userid limit 1 offset 1),
set userTfc_02_Count = (select userCount  from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 1),

set userTfc_03 = (select userTfc from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 2),
set userTfc_03_Count = (select userCount  from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 2),

set userTfc_04 = (select userTfc from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 3),
set userTfc_04_Count = (select userCount  from table_start ts where ts.userid = te.userid order by userTfc limit 1 offset 3) 
where the.userid = 11655