1、查询时间,如果更新时间update_time为空就查创建时间create_time,否则查更新时间update_time

select update_time,create_time, case when (update_time is null or update_time = '') then create_time else update_time end from 表名;

2、当更新时间和创建时间为判断条件且如果更新时间为空就时就根据创建时间来判断,否则用更新时间判断

select * from 表名 t where (
(t.update_time is null or t.update_time = '') and t.create_time > to_date('2020-08-13','yyyy-MM-dd') and t.create_time < to_date('2020-08-14', 'yyyy-MM-dd')) or
(t.update_time > to_date('2020-08-13', 'yyyy-MM-dd') and t.update_time < to_date('2020-08-14', 'yyyy-MM-dd'))

3、上面的sql语句在xml中的表示如下:

<select id="selectByConditions" resultMap="result_Map">
SELECT * FROM 表名
where del_flag=0 and line_type=#{lineType} and (
((update_time is null or update_time = '')
<if test="null != startTime">
and create_time &gt;= #{startTime}
</if>
<if test="null != endTime">
and create_time &lt;= #{endTime}
</if>)
or ( 1=1
<if test="null != startTime">
and update_time &gt;= #{startTime}
</if>
<if test="null != endTime">
and update_time &lt;= #{endTime}
</if>)
)
ORDER BY update_time desc, create_time desc
</select>

力扣中的题目:

给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。

注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。

例如:

| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |

运行你所编写的更新语句之后,将会得到以下表:

| id | name | sex | salary |
|----|------|-----|--------|
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |

SQL:

update salary set sex = case sex when 'f' then 'm' else 'f' end;

力扣:重新格式化部门表

部门表 Department

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| revenue | int |
| month | varchar |
+---------------+---------+
(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。

编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。

查询结果格式如下面的示例所示:

Department 表:
+------+---------+-------+
| id | revenue | month |
+------+---------+-------+
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
+------+---------+-------+ 查询得到的结果表:
+------+-------------+-------------+-------------+-----+-------------+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1 | 8000 | 7000 | 6000 | ... | null |
| 2 | 9000 | null | null | ... | null |
| 3 | null | 10000 | null | ... | null |
+------+-------------+-------------+-------------+-----+-------------+ 注意,结果表有 13 列 (1个部门 id 列 + 12个月份的收入列)。

上述代码的执行语序问题:

1). from子句,然后 group by 子句 ,大致结果是这样(引用图)

2). select子句 , 共13个字段(列),第一列为 id , 即部门编号,这个较好理解。

3). 然后理解剩余12列的值,每有一个id必然会有其对应的12列(月份)的值。当id=1时,进入到经过分组的id为1 的表中,首先为jan_revenue列赋值,遍历该id表的所有行,通过case when 选定对应的revenue或null,然后将选定的这些值sum(详见注解) ,赋值给id的jan_revenue。如此,依次赋值给其他11列。同理,select 其他id值时进入对应id表内...

'不使用类似的聚合函数':不可以。group by 分组后,在mysql中,若不使用聚合函数来提取值,直接select只会select出当前id表的第一行(某个月份),这也意味着在为当前id的12列赋值时,每次都是遍历这一行。结果就是只有与与改行月份对应的那个case when 语句的列会被赋值为revenue,其他皆判断为不符合(即该id的其他列都是null值)

sql:

select id
,sum(case when month='Jan' then revenue end) as Jan_Revenue
,sum(case when month='Feb' then revenue end) as Feb_Revenue
,sum(case when month='Mar' then revenue end) as Mar_Revenue
,sum(case when month='Apr' then revenue end) as Apr_Revenue
,sum(case when month='May' then revenue end) as May_Revenue
,sum(case when month='Jun' then revenue end) as Jun_Revenue
,sum(case when month='Jul' then revenue end) as Jul_Revenue
,sum(case when month='Aug' then revenue end) as Aug_Revenue
,sum(case when month='Sep' then revenue end) as Sep_Revenue
,sum(case when month='Oct' then revenue end) as Oct_Revenue
,sum(case when month='Nov' then revenue end) as Nov_Revenue
,sum(case when month='Dec' then revenue end) as Dec_Revenue
from Department
group by id
order by id

如果不适用聚合函数,则结果如下:

创建时间和更新时间两个选一个的情况和select case when ... then ... else ... end from 表 的使用的更多相关文章

  1. Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段

    为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...

  2. spring data jpa之Auditing 表的创建时间,更新时间自动生成策略

    java实际编程中,几乎每一张表都会有createTime和updateTime字段,spring的优秀之处在于只要用几个注解,就帮我们解决该类问题,具体实现: 1,实体类添加注解: @EntityL ...

  3. JPA注解实体类,给表添加创建时间,更新时间,id的生成以及创建唯一约束

    首先创建一个BaseModel,自动生成创建时间和更新时间 @SuppressWarnings("serial") @MappedSuperclass public class B ...

  4. C#获得指定目录床架时间、更新时间和最后访问时间等信息的代码

    将做工程过程常用的内容片段备份一次,下面的内容内容是关于C#获得指定目录床架时间.更新时间和最后访问时间等信息的内容,希望能对小伙伴们也有用. using System;using System.IO ...

  5. Spring Date Jpa on update current_timestamp 自动维护创建时间和更新时间

    在数据库里设置默认值current_timestamp可以维护创建时间,设置on update current_timestamp 可以维护更新时间.在JPA中应该如何去做呢?这里还是以上篇Topic ...

  6. mysql中创建时间和更新时间的区别

    `create_time` ) NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` ) ) COMMENT '更新时间', 而在界 ...

  7. mysql 表字段 记录创建时间和更新时间

    sql语句创建: CREATE TABLE `NewTable` ( `id` int NOT NULL AUTO_INCREMENT , `name` varchar(20) NOT NULL , ...

  8. django学习-24.创建时间和更新时间的添加

    目录结构 1.前言 2.入参auto_now和入参auto_now_add 2.1.入参auto_now的相关知识点 2.2.入参auto_now_add的相关知识点 3.完整的操作流程 3.1.第一 ...

  9. Ubuntu 16 , 从时间服务器更新时间

    因为在公司的内网,所以不能用Ubuntu默认的服务器去更新时间. 只能改成从网关 10.182.202.2 上取时间 1) 如果没有安装ntp 的话,先安装 apt-get install ntp 2 ...

随机推荐

  1. linux系统磁盘管理(磁盘阵列)

    1.磁盘阵列简介 RAID(Redundant Array of Independent Disks)即独立硬盘冗余阵列,简称磁盘阵列.磁盘阵列是由很多价格较便宜的磁盘,以硬件(RAID卡)或软件(M ...

  2. 飞塔创建IPSec

    5.2和5.4版本飞塔建立IPSec VPN时,必须在两端添加完策略.路由后IPSec才会起来.

  3. WPF权限控制——【2】模块、菜单、按钮

    周末没有工作,没有写博客,因为觉得休息很必要:曾听到一句话是这样说的:"你们得救在乎归回安息:你们得力在乎平静安稳".当我想到太阳没秒钟要燃烧420万吨的燃料时,想到的就是造物主的 ...

  4. 整理我的Git常见问题和命令

    整理我的Git常见问题和命令 目录 整理我的Git常见问题和命令 提交注释规范 合并分支 clone & 切换分支 支持中文路径显示 账户及密码 基于远程分支创建本地分支 提交注释规范 举例: ...

  5. DolphinScheduler 源码分析之 DAG类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  6. 树与图的DFS与BFS

    树的DFS 题目:https://www.acwing.com/problem/content/848/ 代码 #include<bits/stdc++.h> using namespac ...

  7. Windows10与虚拟机中CentOS-7.2进行ftp通信

    首先Linux的IP地址可以通过以下命令获取: ifconfig Windows10上面IP地址通过下面命令获取 ipconfig 你首先要保证你的主机和Linux虚拟机是可以ping通的(ping都 ...

  8. Codeforces Global Round 9 C. Element Extermination

    题目链接:https://codeforces.com/contest/1375/problem/C 题意 给出一个大小为 $n$ 的排列 $a$,如果 $a_i < a_{i+1}$,则可以选 ...

  9. K - Japan(线段树)

    Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...

  10. hdu3905 Sleeping (区间dp)

    Problem Description ZZZ is an enthusiastic ACMer and he spends lots of time on training. He always s ...