MySQL中的两个SQL问题的实现方案

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
一.MySQL中实现date字段上的上周、上月查询
1.创建表 
Create Table: CREATE TABLE `tab` ( 
  `id` int(11) DEFAULT NULL, 
  `name` varchar(50) DEFAULT NULL, 
  `birthday` date DEFAULT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 
2. 
返回上个周的记录: 
(1)    
   select *,DAYNAME(birthday) 
     from tab 
    where birthday <= date_add(now(),interval -(1 + weekday(now())) day) 
      and birthday >= date_add(now(),interval -(8 + weekday(now())) day) 
    order by birthday; 
返回上个月的记录 
(1) 
select *, 
   (case 
    when month(now()) = 1 
    then 
       year(birthday) = year(now()) - 1 and month(birthday) = 12 
    else 
       year(birthday) = year(now())   and month(birthday) = month(now()) -1 end ) as isFlag 
   from tab 
   having isFlag = 1;
---关于case -- when和having的妙用,呵呵以前从没有想过还能这么使用~ 
(2) 
select *,MONTHNAME(birthday) 
     from tab 
    where birthday <= last_day(date_add(now(),interval -1 MONTH)) 
      and birthday >= concat(extract(year_month from date_add(now(),interval -1 MONTH)),'01') 
    order by birthday; 
说明: 
MySQL weekday() 函数和 dayofweek() 类似,都是返回“某天”在一周中的位置。不同点在于参考的标准, weekday:(0 = Monday, 1 = Tuesday, …, 6 = Sunday); dayofweek:(1 = Sunday, 2 = Monday, …, 7 = Saturday)
这里说明下,在itpub论坛上有兄弟给出了
select * from TABLE 
  where PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(register_date,'%Y%m')) = 1
的解决方案,该方案确实能够实现,但是如果在date字段上面加上索引的话,该索引由于使用了函数处理,索引会失效。
 
二.问题描述如下:
A表 
c1  c2  c3 
-------------- 
1 1 a1 
1 2 a2 
1 3 a3 
1 4 a4 
2 1 b1 
2 2 b2 
2 3 b3 
2 4 b4 
3 1 c1 
3 2 c2 
3 3 c3 
3 4 c4 
表中有三组数据,用c1表示,每组数据中有4项内容,用C2表示,每项内容用C3表示 
要求:将每组数据中的第二项复制到第三项 
A表 
c1  c2  c3 
-------------- 
1 1 a1 
1 2 a2 
1 3 a2 
1 4 a4 
2 1 b1 
2 2 b2 
2 3 b2 
2 4 b4 
3 1 c1 
3 2 c2 
3 3 c2 
3 4 c4 
如何用SQL语句操作?
解决方案如下:
1.创建表并插入记录 
create table A(c1 int,c2 int,c3 varchar(10)); 
insert into A values(1,1,'a1'),(1,2,'a2'),(1,3,'a3'),(1,4,'a4'); 
insert into A values(2,1,'b1'),(2,2,'b2'),(2,3,'b3'),(2,4,'b4'); 
insert into A values(3,1,'c1'),(3,2,'c2'),(3,3,'c3'),(3,4,'c4'); 
mysql&gt; select * from A; 
+------+------+------+ 
| c1   | c2   | c3   | 
+------+------+------+ 
|    1 |    1 | a1   | 
|    1 |    2 | a2   | 
|    1 |    3 | a3   | 
|    1 |    4 | a4   | 
|    2 |    1 | b1   | 
|    2 |    2 | b2   | 
|    2 |    3 | b3   | 
|    2 |    4 | b4   | 
|    3 |    1 | c1   | 
|    3 |    2 | c2   | 
|    3 |    3 | c3   | 
|    3 |    4 | c4   | 
+------+------+------+ 
12 rows in set (0.00 sec) 
2.实现过程 
(1)实现分组排名
初始化用户变量:set @c1=1; 
mysql&gt; select result.c1,result.c1,result.c3, 
                    result.rownum, 
                    result.rank 
         from    
              (select ff.c1,ff.c2,ff.c3, 
                      @rownum:=@rownum+1 rownum, 
                      if(@c1=ff.c1,@rank:=@rank+1,@rank:=1) as rank, 
                      @c1 := ff.c1   
                 from  (select c1,c2,c3 
                         from A 
                         group by c1,c2,c3 
                         order by c1,c3,c3) ff, 
                        (select @rownum :=0,@rank:=0,@c1=null) tt) result; 
+------+------+------+--------+------+ 
| c1   | c1   | c3   | rownum | rank | 
+------+------+------+--------+------+ 
|    1 |    1 | a1   |      1 |    1 | 
|    1 |    1 | a2   |      2 |    2 | 
|    1 |    1 | a3   |      3 |    3 | 
|    1 |    1 | a4   |      4 |    4 | 
|    2 |    2 | b1   |      5 |    1 | 
|    2 |    2 | b2   |      6 |    2 | 
|    2 |    2 | b3   |      7 |    3 | 
|    2 |    2 | b4   |      8 |    4 | 
|    3 |    3 | c1   |      9 |    1 | 
|    3 |    3 | c2   |     10 |    2 | 
|    3 |    3 | c3   |     11 |    3 | 
|    3 |    3 | c4   |     12 |    4 | 
+------+------+------+--------+------+ 
12 rows in set (0.00 sec) 
(2)将排名后的记录插入到两个临时表中 
mysql&gt; create  table temp(c1 int,c2 int,c3 varchar(10),rownum int,rank int); 
Query OK, 0 rows affected (0.13 sec) 
mysql&gt; insert into temp 
    -&gt; select result.c1,result.c2,result.c3, 
              result.rownum, 
              result.rank 
        from  (select ff.c1,ff.c2,ff.c3, 
                      @rownum:=@rownum+1 rownum, 
                      if(@c1=ff.c1,@rank:=@rank+1,@rank:=1) as rank, 
                      @c1 := ff.c1 
                from 
                    (select c1,c2,c3 
                      from A 
                     group by c1,c2,c3 
                     order by c1,c3,c3) ff, 
                    (select @rownum :=0,@rank:=0,@c1=null) tt)result; 
Query OK, 12 rows affected (0.03 sec) 
Records: 12  Duplicates: 0  Warnings: 0 
mysql&gt; select * from temp; 
+------+------+------+--------+------+ 
| c1   | c2   | c3   | rownum | rank | 
+------+------+------+--------+------+ 
|    1 |    1 | a1   |      1 |    1 | 
|    1 |    2 | a2   |      2 |    2 | 
|    1 |    3 | a3   |      3 |    3 | 
|    1 |    4 | a4   |      4 |    4 | 
|    2 |    1 | b1   |      5 |    1 | 
|    2 |    2 | b2   |      6 |    2 | 
|    2 |    3 | b3   |      7 |    3 | 
|    2 |    4 | b4   |      8 |    4 | 
|    3 |    1 | c1   |      9 |    1 | 
|    3 |    2 | c2   |     10 |    2 | 
|    3 |    3 | c3   |     11 |    3 | 
|    3 |    4 | c4   |     12 |    4 | 
+------+------+------+--------+------+ 
12 rows in set (0.00 sec) 
mysql&gt; create table temp2 as select * from temp; 
Query OK, 12 rows affected (0.16 sec) 
Records: 12  Duplicates: 0  Warnings: 0 
mysql&gt; select * from temp2; 
+------+------+------+--------+------+ 
| c1   | c2   | c3   | rownum | rank | 
+------+------+------+--------+------+ 
|    1 |    1 | a1   |      1 |    1 | 
|    1 |    2 | a2   |      2 |    2 | 
|    1 |    3 | a3   |      3 |    3 | 
|    1 |    4 | a4   |      4 |    4 | 
|    2 |    1 | b1   |      5 |    1 | 
|    2 |    2 | b2   |      6 |    2 | 
|    2 |    3 | b3   |      7 |    3 | 
|    2 |    4 | b4   |      8 |    4 | 
|    3 |    1 | c1   |      9 |    1 | 
|    3 |    2 | c2   |     10 |    2 | 
|    3 |    3 | c3   |     11 |    3 | 
|    3 |    4 | c4   |     12 |    4 | 
+------+------+------+--------+------+ 
12 rows in set (0.00 sec) 
(3)更新记录 
mysql&gt; update temp2,temp set temp2.c3=temp.c3 where temp.c1=temp2.c1 and temp2.rank=2 and temp.rank=3; 
Query OK, 3 rows affected (0.05 sec) 
Rows matched: 3  Changed: 3  Warnings: 0 
mysql&gt; update temp2,temp set temp2.c3=temp.c3 where temp.c1=temp2.c1 and temp2.rank=3 and temp.rank=2; 
Query OK, 3 rows affected (0.08 sec) 
Rows matched: 3  Changed: 3  Warnings: 0 
mysql&gt; select * from temp2; 
+------+------+------+--------+------+ 
| c1   | c2   | c3   | rownum | rank | 
+------+------+------+--------+------+ 
|    1 |    1 | a1   |      1 |    1 | 
|    1 |    2 | a3   |      2 |    2 | 
|    1 |    3 | a2   |      3 |    3 | 
|    1 |    4 | a4   |      4 |    4 | 
|    2 |    1 | b1   |      5 |    1 | 
|    2 |    2 | b3   |      6 |    2 | 
|    2 |    3 | b2   |      7 |    3 | 
|    2 |    4 | b4   |      8 |    4 | 
|    3 |    1 | c1   |      9 |    1 | 
|    3 |    2 | c3   |     10 |    2 | 
|    3 |    3 | c2   |     11 |    3 | 
|    3 |    4 | c4   |     12 |    4 | 
+------+------+------+--------+------+ 
12 rows in set (0.00 sec) 
(4)temp2中的记录即是想要的结果。


本文转自 yubowei 51CTO博客,原文链接:http://blog.51cto.com/samyubw/232080
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
17天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
13 0
|
10天前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
15 1
|
17天前
|
SQL 关系型数据库 MySQL
【MySQL】慢SQL分析流程
【4月更文挑战第1天】【MySQL】慢SQL分析流程
|
20天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
66 0
|
20天前
|
SQL 关系型数据库 MySQL
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
36 1
|
10天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法
|
28天前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
81 1
|
23天前
|
Ubuntu 关系型数据库 MySQL
Ubuntu 中apt 安装MySQL数据库
Ubuntu 中apt 安装MySQL数据库
66 0
|
2天前
|
关系型数据库 MySQL Linux
Linux联网安装MySQL Server
Linux联网安装MySQL Server
10 0