Explain ThinkPHP6 joint table aggregation query from examples
A few days ago, a friend asked about an aggregation query of a joint table. Thinking that this problem may be difficult for many novices to deal with, I would like to share it now.
We have two data tables:
bus table:
##user table:
Requirements: Count the number of people getting on the bus
How to achieve it?Step One: Join Tables
#This scenario definitely requires joining two data tables. Let’s not consider statistics first. Join two data tables.$data=Db::name('user')->alias('a')->join('bus b','a.user_id=b.user_id')->select()->toArray();
Step 2: Aggregation query
Before doing the aggregation query, let’s take a look at the official Manual tutorial.$data=Db::name('user')->alias('a')->field('count(b.user_id) AS c'')->join('bus b','a.user_id=b.user_id')->select()->toArray();
$data=Db::name('user')->alias('a')->field('count(b.user_id) AS c')->join('bus b','a.user_id=b.user_id')->group('a.user_id')->select()->toArray();
The third point: Pay attention to the situation
In the above query statement, if mysql is version 5.7, you need to pay special attention. For example, if you add a.* to the field in mysql5.7, an error will be reported:$data=Db::name('user')->alias('a')->field('a.*,count(b.user_id) AS c')->join('bus b','a.user_id=b.user_id')->group('a.user_id')->select()->toArray(); [object Object]
We can modify the mysql configuration:
The above is the detailed content of Explain ThinkPHP6 joint table aggregation query from examples. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

