这是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 ,我需要更新:
你能帮助我吗?
提前感谢您的帮助,非常感谢。
答案 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