数据库语句指令跟年份月份有关的代码

网络评论阅读模式

示例一

mysql数据库查询某一年内各月份数据,按月份分组

sql语句:

select month(date_no) as month,sum(amount) from earnings where year(date_no) = 2017  group by month (date_no)

表名是earnings, 时间字段是date_no;这里查询的是2017年内 1-12月份每个月 amount这一列的总和;

 

示例:

表结构大致如下:

select amount,date_no,short_no,worker_id,procedure_id from earnings limit 0,10;
+----------+---------------------+----------+-----------+----------------------------------+
| amount    | date_no                      | short_no | worker_id | procedure_id |
+----------+---------------------+----------+-----------+----------------------------------+
| 3.60         | 2017-12-23 20:37:33 | 111       |        23      |           31 |
| 15000.00 | 2017-12-23 20:43:12 | 8806     | NULL       |           41 |
| 600.00     | 2017-12-23 20:46:42 | 8826     |        24     |           41 |
| 600.00     | 2017-12-23 20:46:42 | 8826     |        25     |           41 |
| 120.00     | 2017-12-23 20:53:22 | 8826     | NULL       |           40 |
| 120.00     | 2017-12-23 21:29:11 | 12         |        27     |           34 |
| 48.00       | 2017-12-23 21:32:34 | 12         | NULL       |           35 |
| 12.00       | 2017-12-23 21:35:49 | 12         |        30     |           38 |
| 1.00         | 2017-12-23 21:51:33 | 1           |        23     |           31 |
| 12.00       | 2017-12-23 22:13:40 | 1           |        30     |           37 |
+----------+---------------------+----------+-----------+--------------------------+

 

select month(date_no) as month,sum(amount) from earnings where year(date_no) = 2018 and  group by month (date_no);
+-------+-------------+
| month | sum(amount) |
+-------+-------------+
|     1 | 161753.09   |
|     2 | 1020.00     |
|     3 | 34422.80    |
|     4 | 10498.80    |
|     5 | 1670.40     |
|     6 | 1182.00     |
|     7 | 195.20      |
|     8 | 161404.65   |
|     9 | 117190.12   |
|    10 | 138.20      |
+-------+-------------+

示例二

Access两查询匹配相同,时间在同一月

这里假设时间是按20160101这种bai格式登记为文本数据类型的

select a.* from 查询du1 a where 
exists (select 1 from 查询2 b 
where left(b.时间,6)=left(a.时间,6));

如果时间的数zhi据类型是(日期/时间)则这样写

select a.* from 查询1 a where 
exists (select 1 from 查询2 b 
where format(b.时间,'yyyymm')=format(a.时间,'yyyymm'));

 

示例三

ACCESS如何按年或按月查询

--按年
select * from 表名 where format(字段名,"yyyy")=你输入的年份

--按月
select * from 表名 where format(字段名,"mm")=你输入的月份

--按年月,注意你的年月要转换为200606类似的格式
select * from 表名 where format(字段名,"yyyymm")=你输入的年月

--或者直接用year()、Month()函数。

select * from 表名 where year(字段名)=你输入的年 and month(字段名)=你输入的月

示例四

access如何查询指定月份之前的记录

目的是使得出生日期的月份bai值小于或等于输入查du询的月份值。
方法1:字段zhi为[出生日期],
条件dao行输入:Month([学生表]![出生日期])<=Val([输入要查询的月份:])
方法2:字段为表达式:Month([学生表]![出生日期])
条件行输入:<=Val([输入要查询的月份:])
都是将字段月份值与你输入的值比较,只是中间多了个函数取值而已。两种函数返回的值都是可以比较的。

 

用month函数取月份,例如查询出生日期在1月到3月之间的,查询条件可以写成
month([出生日期]) between 1 and val([月份])

 

 

 

继续阅读
weinxin
我的微信
运营不易,
感谢支持!
公式库网
  • 本文由 发表于 2020年10月2日 13:53:20
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202010/shujukuyujuzhilinggennianfenyuefenyouguandedaima.html

发表评论