MySQL常用查询

Posted 知其黑、受其白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL常用查询相关的知识,希望对你有一定的参考价值。

mysql 根据两个字段分组,根据两个字段排序

获取年和季度分组,并设置年排序和季度排序。

SELECT year, quarter FROM report
WHERE FIND_IN_SET(`quarter`, 'Q1,Q2,Q3,Q4')
GROUP BY `year`, `quarter`
ORDER BY `year` DESC, `quarter` DESC

多 where 应用

select `create_time`,`field_id`,`field`, `name`, `form_type`, `order_id`, `order_list`, `is_show` 
from `hhir_crm_admin_field` 
where (`types` = 'crm_upload_quarter' and `valid` = 1 and `is_filter` = 0)
ORDER BY `order_id` ASC,`create_time` DESC

MySQL 根据统计 ID 出现次数和根据年度、季度分组

SELECT count(fund_id) AS count_fund_id, year, quarter
FROM report
GROUP BY fund_id, year, quarter

MySQL 分组排序过滤

未过滤的数据

SELECT year, quarter FROM report
GROUP BY `year`, `quarter`
ORDER BY `year` DESC, `quarter` DESC


过滤的数据

SELECT year, quarter FROM report
GROUP BY `year`, `quarter`
HAVING `quarter` NOT IN ('Q1', 'Q2', 'Q3', 'Q4')
AND `quarter` != ''
ORDER BY `year` DESC, `quarter` DESC

以上是关于MySQL常用查询的主要内容,如果未能解决你的问题,请参考以下文章

mysql按年度季度月度周日统计查询的sql语句

mysql 按年度季度月度周日SQL统计查询

【Mysql】按季度查询结果数据累加

MySQL关于时间的查询,比如本月,本年,本季度

php按当前年份、季度、当月,查询mysql数据库并输出数组

MySQL10查询某年,某季度,某月,某周,某天的数据