PHP多表聯查count重複的問題

2021-10-07 02:29:30 字數 721 閱讀 6539

多表聯查count資料來進行統計,會出現重複的資料,這時候在查詢中加上distinct來返回不同的值.

db::name(『member』)

->alias(『a』)

->field(『count( distinct b.id) as followcount,count(distinct c.id) as articlecount,count(distinct d.id) as readbookcount,count(distinct e.id) as commentscount』)

->leftjoin(『member_follow b』,『a.id = b.uid and b.delete_time is null』)

->leftjoin(『article c』,『a.id = c.uid and c.delete_time is null and c.art_status = 1』)

->leftjoin(『read_book d』,『a.id = d.uid and d.delete_time is null』)

->leftjoin(『order_score e』,『a.id = e.uid and e.delete_time is null』)

->where(『a.delete_time』,null)

->group(『a.id』)

->select();

如果需要檢視distinct和group by的區別可以去

進行**

mysql 多表聯查 MySQL的多表聯查

今天是周二,我們一起來研究下mysql的多表聯查啊。或許你也知道,表之間的關係有 1對1 1對多 多對多。然後.1.巢狀查詢 乙個查詢的結果是另外sql查詢的條件 如 查詢stu表中年齡最大的是誰?mysql select from stu where age select max age from...

簡單的多表聯查sql

mysql資料庫預設建立的test資料庫下實踐 建立兩張表 users 和 products use test create table test users user id intnot null auto increment,username varchar 30 primary key use...

oracle中多表聯查的寫法

最好這樣,join多表鏈結最好使用數字型別,且必須為主鍵,否則可能會鏈結失敗 select from 表1 inner join 表2 on 表1.欄位號 表2.欄位號 inner join 表3 on 表1.欄位號 表3.欄位號 inner join表4 on member.欄位號 表4.欄位號例...