MySql實現根據某個條件去重求和

具體的可以看下  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	

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章