Reputation: 1407
This is my table_start on MySQL database :
+--------+---------+-----------+
| userID | userTfc | userCount |
+--------+---------+-----------+
| 11655 | SN10 | 45 |
| 11655 | SN16 | 80 |
| 11655 | SN24 | 796 |
| 11655 | SN35 | 56 |
+--------+---------+-----------+
I need update the table_end on the same MySQL database in this mode :
+--------+------------+------------------+------------+------------------+------------+------------------+------------+------------------+
| 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 |
+--------+------------+------------------+------------+------------------+------------+------------------+------------+------------------+
For the same userID I need update :
Can you help me?
Thank you in advance for any help, really appreciated.
Upvotes: 0
Views: 53
Reputation: 1256
You can try below query. Use order by , limit and offset of mysql. Order by your query then use limit to get one record and use offset to pick line number.
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
Upvotes: 0
Reputation: 570
First execute this query for your first table
$query1 = "SELECT userTfc,userCount FROM table_start WHERE userID='11655' ";
I am assuming after above query you got the result in an array lets say $result. Now you can update your second table using loop. For example :
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);
}
}
Upvotes: 1