mysql 的一点点记录
以后再来整理。
-- 查询一个学校的报修单数
SELECT
s.id AS schoolId , -- 学校ID
COUNT(i.id) as cntId, -- 报修单数
IFNULL(t1.nohandlecnt,0) AS nohandlecnt,
IFNULL(t2.overcnt,0) AS overcnt,
ROUND(overcnt * 100 /COUNT(i.id),1) AS okRate
FROM
`t_issue` i
LEFT JOIN (
-- 完成报修单数
SELECT
s.id AS schoolId , -- 学校ID
IFNULL(COUNT(i.id),0) as overcnt -- 完成的报修单数
FROM
`t_issue` i
INNER JOIN t_school s ON i.school_id = s.id
WHERE i.completed_on IS NOT NULL and
i.acknowledged_on IS NOT NULL
GROUP BY
i.school_id
) t2 ON t2.schoolId = i.school_id
LEFT JOIN (
-- 未处理数
SELECT
s.id AS schoolId , -- 学校ID
IFNULL(COUNT(i.id),0) as nohandlecnt -- 未处理报修单数 FROM
`t_issue` i
INNER JOIN t_school s ON i.school_id = s.id
WHERE
i.acknowledged_on IS NULL AND
i.completed_on IS NULL GROUP BY
i.school_id
) t1 ON t1.schoolId = i.school_id
INNER JOIN t_school s ON i.school_id = s.id
GROUP BY
i.school_id
另一种保留小数的方法。
SELECT
TRUNCATE (avg(attitude), ) AS attitude,
TRUNCATE (avg(timeliness), ) AS timeliness,
TRUNCATE (avg(professionality), ) AS professionality,
TRUNCATE (avg(overall), ) AS overall
FROM (
SELECT
school_id,s.sc_name,
TRUNCATE (avg(attitude),) as attitude ,
TRUNCATE (avg(timeliness), ) as timeliness,
TRUNCATE (avg(professionality), ) as professionality,
TRUNCATE (avg(overall), ) as overall
FROM (
SELECT
t_repairmen.school_id ,
TRUNCATE (avg(attitude),) AS attitude,
TRUNCATE (avg(timeliness),) AS timeliness,
TRUNCATE (avg(professionality),) AS professionality ,
TRUNCATE (
(
avg(attitude) + avg(timeliness) + avg(professionality)
) / , ) AS overall
FROM `t_issue_rating`
INNER JOIN t_repairmen ON t_repairmen.id = t_issue_rating.repairmen_id
WHERE `is_dummy` = '' GROUP BY
repairmen_id,t_repairmen.school_id ) t
INNER JOIN t_school s ON t.school_id = s.id
WHERE
=
<if test="conditions.schools!=null">
AND school_id in (${conditions.schools})
</if>
GROUP BY school_id
ORDER BY
s.id DESC
) y
当遇到count 和group by 在一起,统计出的结果不正确时。
SELECT COUNT(DISTINCT id) FROM `xx` WHERE = group by id
两个表的count值相加
select
(select count(*) from bumen)+(select count(*) from mrs) as sum_count
查询用户所在的排名
SELECT
concat(
(
@dddd_row_num :=@dddd_row_num +
) +, ''
) AS row_id
FROM
history,
(SELECT @dddd_row_num := ) AS foo
WHERE
=
order by mp_send_time DESC
limit offset
各种统计count:
可以在统计count 的时候,将不符合条件的数排除在外
count(if( e.ext5 is NOT NULL ,true,null)) erijCount,
SELECT
c.classes_id AS examId,
i.eng_item_name AS examSubject,
c.classes_name AS examName,
eng_apply_start AS registStart,
eng_apply_end AS registEnd,
classes_status AS examStatus,
classes_fee AS examFee,
(
CASE
WHEN i.ext2 IS NOT NULL THEN
i.ext2
ELSE END
) AS examFee1,
A.realityApplyCount * classes_fee AS shouldApplyFee,
A.realityApplyFee,
A.realityApplyCount
FROM
eng_classes c
INNER JOIN eng_item i ON i.eng_id = c.eng_id
LEFT JOIN exam_examinee ON c.classes_id = examinee_exam_id
AND (
examinee_exam_status != ''
OR ISNULL(examinee_exam_status)
)
AND examinee_apply_type = 'english'
LEFT JOIN (
SELECT
classes_id AS Id,
count( IF (
examinee_pay_status = "",
TRUE,
NULL
)
) * classes_fee AS realityApplyFee,
count(DISTINCT examinee_id) realityApplyCount
FROM
eng_classes
INNER JOIN exam_examinee ON classes_id = examinee_exam_id
AND (
examinee_exam_status != ''
OR ISNULL(examinee_exam_status)
)
AND examinee_apply_type = 'english'
GROUP BY
classes_id
) AS A ON classes_id = A.Id
WHERE
=
GROUP BY
classes_id
还有这样一种,sum去叠加,中间加判断的。
SELECT
pro_apply_id AS examId ,
pro_apply_name AS examSubject,
pro_apply_start AS registStart ,
pro_apply_end AS registEnd ,
pro_apply_fee AS examFee ,
A.realityApplyCount * pro_apply_fee as shouldApplyFee,
A.realityApplyFee,
A.realityApplyCount
FROM pro_apply
LEFT JOIN exam_examinee ee ON pro_apply_id = ee.exam_item_id
AND examinee_apply_type = 'profession'
LEFT JOIN (
SELECT
apply_id AS Id ,
SUM(if(examinee_pay_status = "",item_fee,)) AS realityApplyFee ,
count(DISTINCT examinee_id) realityApplyCount
FROM
pro_item
INNER JOIN exam_examinee ee ON item_id = examinee_exam_id
AND (examinee_exam_status != '' OR ISNULL(examinee_exam_status))
AND examinee_apply_type = 'profession'
GROUP BY apply_id
) AS A ON A.Id LIKE CONCAT('%' ,pro_apply_id, '%')
mysql 的一点点记录的更多相关文章
- MYSQL删除表的记录后如何使ID从1开始
MYSQL删除表的记录后如何使ID从1开始 MYSQL删除表的记录后如何使ID从1开始 http://hi.baidu.com/289766516/blog/item/a3f85500556e2c09 ...
- mysql防止重复插入记录方法总结
mysql防止重复插入记录方法总结 防止mysql重复插入记录的方法有很多种,常用的是ignore,Replace,ON DUPLICATE KEY UPDATE,当然我们也可以在php中加以判断了. ...
- navicat MySQL 只有1000条记录
/*************************************************************************** * navicat MySQL 只有1000条 ...
- mysql 查询一条记录的下一条和上一条记录
如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...
- mysql中删除重复记录,只保留一条
表结构如下: mysql> desc test1; +--------------+------------------+------+-----+---------+------------- ...
- 【mysql】索引优化记录
基础知识 Innodb存储引擎 支持行锁 支持事务: Myisam存储引擎 只支持表锁: 不支持事务: 常见索引列表 独立的列 前缀索引(索引选择性) 多列索引(并不是多个单列索引,索引顺序很重要) ...
- mysql 删除表中记录
一.清除mysql表中数据 delete from 表名;truncate table 表名;不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以 ...
- MySQL Limit 限定查询记录数
MySQL Limit 限定查询记录数 MySQL LIMIT MySQL 中 LIMIT 关键字用于限定查询记录返回最大数目. 语法: ... LIMIT offset , rows 该语法中,of ...
- MySQL单表最大记录数不能超过多少?
MySQL单表最大记录数不能超过多少? 很多人困惑这个问题.其实,MySQL本身并没有对单表最大记录数进行限制,这个数值取决于你的操作系统对单个文件的限制本身. 从性能角度来讲,MySQL单表数据不要 ...
随机推荐
- 域名无法解析 Linux临时或永久修改DNS
最近给VPS重装了系统,因为服务商不提供DHCP,所以只好手动设置IP和DNS Server.悲催的是系统重装的时候忘记了输入DNS Server,最后导致进去系统后,各种域名无法解析. Linux中 ...
- 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值
上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...
- 如何使php页面中不再出现NOTICE和DEPRECATED的错误提示
在php.ini配置文件中修改: error_reporting=E_ALL & ~E_NOTICE & ~E_DEPRECATED 亲测有效,拿去用吧
- 兼容火狐,ie8的 js urlencode和urldecode
function UrlEncode(str)//url编码{ var i,temp,p,q; var result=""; for(i=0;i<str.length;i++ ...
- 如何用python的装饰器定义一个像C++一样的强类型函数
Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...
- Android开发之探秘蓝牙隐藏API
这次讲得深入些,探讨下蓝牙方面的隐藏API.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢 ...
- Spring MVC POST中文乱码解决方案
spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题,具体配置如下: ...
- sleep()和usleep()的使用和区别
在iOS中 如果 在主线程中用这2个 都会 对 主线程 造成 阻塞 具体区别 如下 Linux 中的 代码 为例 函数名: sleep头文件: #include <windows.h> ...
- windows平台安装redis服务
有时候我们需要在windows平台上使用redis作为缓存服务,这个时候就需要将redis安装为window服务. 现在将我安装过程记录下来,方便自己或者有类似需求的同学参考.主要是参考网上现有的例子 ...
- mysql数据库以加索引方式提高性能
数据库查询速率慢的情况下可以给对应的表加上对应的索引,能够有效的提高查询效率,mysql数据库添加索引的SQL入下: ALTER TABLE `table_name` ADD INDEX index_ ...