Home > Backend Development > PHP Tutorial > ThinkPHP multi-table query-if field A is the same, add field B_PHP tutorial

ThinkPHP multi-table query-if field A is the same, add field B_PHP tutorial

WBOY
Release: 2016-07-13 10:22:29
Original
1305 people have browsed it

ThinkPHP multi-table query-if field A is the same, add field B

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847862.htmlTechArticleThinkPHP multi-table query - if field A is the same, add field B in one item, a query table is required The user_id field in tr_product, if the user_id is the same, then the corresponding money field...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template