In a project, you need to query the user_id field in the table tr_product. If the user_id is the same, add the corresponding money fields. The database screenshot is as follows:
Implementation code:
$Model = D("Model');
$res =$Model->query("SELECT user_id,sum(money) from tr_order GROUP BY user_id");
Result:
array (size=2)
0 =>
array (size=2)
'user_id' => string '72' (length=2)
'sum(money)' => string '100677.00' (length=9)
1 =>
array (size=2)
'user_id' => string '89' (length=2)
'sum(money)' => string '34.00' (length=5)
Technical extensions:
Using the query function provided by ThinkPHP, we can directly enter our sql statement.
GROUP BY Group by user_id.
sum() function: accumulates the money field value.
A PHPer and a Linuxer