您的位置:首页 > 数据库

SQL如何Count Distinct过的数据

2016-02-03 15:15 274 查看
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是必须的 临时表需要名字 不然会报错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: