比如一个表里面有多个张三、多个李四,我们调用数据时碰到重复的多个张三时只调一条数据,多个李四也只调一条数据。
查询相同姓名(name字段)的最小id行
1、原生sql方式
SELECT a.* from visit as a where not exists (select 1 from visit as b where b.name = a.name and (b.id < a.id))
2、Thinkphp6方式
->whereRaw('not exists (select 1 from visit as b where b.name = a.name and (b.id < a.id))')
3、我的使用示例:
SELECT a.*, ( SELECT COUNT(*) AS think_count FROM `visit` WHERE ((url = a.url)) AND `visit`.`delete_time` IS NULL ) AS pv_count FROM `visit` `a` WHERE ( `a`.`create_time` BETWEEN 1609430400 AND 1612108799 AND( NOT EXISTS( SELECT 1 FROM visit AS b WHERE b.url = a.url AND(b.id < a.id) ) ) ) AND `a`.`delete_time` IS NULL ORDER BY `pv_count` ASC, `a`.`id` DESC