SQL如何Count Distinct过的数据

distinct:

<pre name="code" class="sql">SELECT distinct t1.*,t2.industryId  FROM positions t1 ,planteddetails t2, landplanted t3 where t3.landId = t1.id and t2.plantedId = t3.id
and (t1.landType like '%粮%' or t1.gatherId like '%粮%') and t2.industryId ='1'


 

直接count会报错


SELECT count((distinct t1.*,t2.industryId  FROM positions t1 ,planteddetails t2, landplanted t3) where t3.landId = t1.id and t2.plantedId = t3.id
and (t1.landType like '%粮%' or t1.gatherId like '%粮%') and t2.industryId ='1'

所以我们可以换个思路


select count(*) from (SELECT distinct t1.*,t2.industryId  FROM positions t1 ,planteddetails t2, landplanted t3 where t3.landId = t1.id and t2.plantedId = t3.id
and (t1.landType like '%粮%' or t1.gatherId like '%粮%') and t2.industryId ='1') as total

直接把distinct的结果作为一张表count

这里as total是必须的 临时表需要名字 不然会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL中,使用COUNT(DISTINCT 列名)可以统计某个列中不重复的唯一值的数量。例如,对于表格fromtable,可以使用以下查询语句统计userType为1和2的不重复userid的数量: ``` SELECT COUNT(DISTINCT case when userType=1 then userid else null end) as 类型1, COUNT(DISTINCT case when userType=2 then userid else null end) as 类型2 FROM fromtable ``` 这个查询语句会返回两个列,分别是类型1和类型2对应的不重复userid的数量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [sql count()加distinct和条件去重统计](https://blog.csdn.net/u011974797/article/details/112554220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [count()和distinct关键字的使用](https://blog.csdn.net/qq_39826207/article/details/108537874)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量](https://blog.csdn.net/weixin_55674264/article/details/125471753)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值