ThinkPHP 多表查询-如果字段A相同,则把字段B相加
在一个项目中,需要查询表tr_product中的user_id字段,如果user_id相同,则把其对应的money字段相加,数据库截图如下:
实现代码:
$Model = D("Model');
$res =$Model->query("SELECT user_id,sum(money) from tr_order GROUP BY user_id");
结果:
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)
技术扩展:
使用ThinkPHP提供的query函数,能够直接输入我们的sql语句。
GROUP BY 按user_id分组。
sum()函数:并对的money字段值进行累加。
A PHPer and a Linuxer
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor