MySql实现根据某个条件去重求和

本文介绍了如何使用SQL进行数据查询时的去重操作,包括直接查询、去重查询及去重后的计数操作。通过对比三种不同场景下的SQL语句,展示了如何利用DISTINCT关键字来优化查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体的可以看下  distinct 具体用法

   

//第一图	直接查询出所有的id
			SELECT
 d.id
				FROM
					t_device  d
				LEFT JOIN	t_device_collect_state a on d.id = a.device_collect_id
				LEFT JOIN t_group b on d.group_id = b.id
				LEFT JOIN t_farm  c on d.farm_id = c.id
				where
					d.del_flag = '0' and d.type =2  and c.id =1
order by b.create_time desc	



//第二图 查询所有id(已经去重)
			SELECT
distinct d.id
				FROM
					t_device  d
				LEFT JOIN	t_device_collect_state a on d.id = a.device_collect_id
				LEFT JOIN t_group b on d.group_id = b.id
				LEFT JOIN t_farm  c on d.farm_id = c.id
				where
					d.del_flag = '0' and d.type =2  and c.id =1
order by b.create_time desc	

//第三图  求和(去重求和)
		SELECT
	count(distinct d.id)
				FROM
					t_device  d
				LEFT JOIN	t_device_collect_state a on d.id = a.device_collect_id
				LEFT JOIN t_group b on d.group_id = b.id
				LEFT JOIN t_farm  c on d.farm_id = c.id
				where
					d.del_flag = '0' and d.type =2  and c.id =1
order by b.create_time desc	

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值