mysql实现name姓名多个相同的数据只取⼀条⽐如⼀个表⾥⾯有多个张三、多个李四,我们调⽤数据时碰到重复的多个张三时只调⼀条数据,多个李四也只调⼀条数据。查询相同姓名(name字段)的最⼩id⾏
1、原⽣sql⽅式
mysql删除重复的数据保留一条SELECT a.*from visit as a where not exists (select1from 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` BETWEEN1609430400AND1612108799AND(
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论