SQL如何Count Distinct过的数据
2016-02-03 15:15
274 查看
distinct:
直接count会报错
所以我们可以换个思路
直接把distinct的结果作为一张表count
这里as total是必须的 临时表需要名字 不然会报错
<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是必须的 临时表需要名字 不然会报错
相关文章推荐
- java数据库编程——执行查询操作(二)
- mysql or查询优化
- Redis学习手册(Sorted-Sets数据类型)
- MYSQL获取自增ID的四种方法
- MySQL 中隔离级别 RC 与 RR 的区别
- linux 下 创建多个redis实例,并且配置主从复制
- 无法打开MongoDB 28017监控页面
- memcache缓冲雪崩现象
- mysql Blob和Text字段过程错误
- mysql+redis
- oracle共享池
- MySQL把数据删除后id从1开始
- php mongodb扩展安装
- oracle的背景
- SQL 语言划分
- Oracle 死锁的检测查询及处理
- mysql 如何手动插入数据blob类型数据
- TRUNCATE TABLE 与 DELETE区别
- mysql-5.7.10-winx64免安装版配置时碰到的问题
- (26)SQLite集成与用法