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

简介:
一.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
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
运维 监控 关系型数据库
MySQL高可用方案:MHA与Galera Cluster对比
本文深入对比了MySQL高可用方案MHA与Galera Cluster的架构原理及适用场景。MHA适用于读写分离、集中写入的场景,具备高效写性能与简单运维优势;而Galera Cluster提供强一致性与多主写入能力,适合对数据一致性要求严格的业务。通过架构对比、性能分析及运维复杂度评估,帮助读者根据自身业务需求选择最合适的高可用方案。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1890 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1306 156
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
SQL 监控 关系型数据库
查寻MySQL或SQL Server的连接数,并配置超时时间和最大连接量
以上步骤提供了直观、实用且易于理解且执行的指导方针来监管和优化数据库服务器配置。务必记得,在做任何重要变更前备份相关配置文件,并确保理解每个参数对系统性能可能产生影响后再做出调节。
1031 11
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
715 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(中)
使用MYSQL Report分析数据库性能
846 156
|
缓存 监控 关系型数据库
使用MYSQL Report分析数据库性能(上)
最终建议:当前系统是完美的读密集型负载模型,优化重点应放在减少行读取量和提高数据定位效率。通过索引优化、分区策略和内存缓存,预期可降低30%的CPU负载,同时保持100%的缓冲池命中率。建议每百万次查询后刷新统计信息以持续优化
883 161
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。

推荐镜像

更多