好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Yii2去除重复数据

要求:去除数据库中重复的xid数据,并且只保留一条最大的id的数据

解决
User::find()->select(“id”)->groupBy([‘xid’])->where([‘<>’,’xid’, 0])
->andWhere([‘<>’,’province_id’, 0])
->orderBy(‘xid DESC’)->asArray()->all();

注意:groupBy是将重复的xid打组,orderBy(‘xid DESC’)是关键,倒叙排列后即可将最大的排到最前面。

执行完后可以在数据库中执行SQL语句查看效果

SQL语句:select xid, count(xid) as count from user where xid !=0 and c_province_id !=0 group by xid having count(xid) >1 order by xid DESC

用having统计一下重复数目

查看更多关于Yii2去除重复数据的详细内容...

  阅读:1429次