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. 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest PART(10/12)

    $$2017-2018\ ACM-ICPC,\ Asia\ Daejeon\ Regional\ Contest$$ \(A.Broadcast\ Stations\) \(B.Connect3\) ...

  2. java.awt.event.MouseEvent鼠标事件的定义和使用 以及 Java Swing-JTextArea的使用

    最近发现一个CSDN大佬写的Java-Swing全部组件的介绍:Java Swing 图形界面开发(目录) JTextArea 文本区域.JTextArea 用来编辑多行的文本.JTextArea 除 ...

  3. Best Reward && Girls' research

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

  4. XHXJ's LIS HDU - 4352 最长递增序列&数位dp

    代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...

  5. httprunner(11)运行测试报告

    前言 受益于pytest的集成,HttpRunner v3.x可以使用pytest所有插件,包括pytest-html和allure-pytest,也可以实现这2种方式的报告 内置html报告 pyt ...

  6. OpenStack Train版-9.安装neutron网络服务(计算节点)

    在计算节点安装neutron网络服务(computel01计算节点192.168.0.20)安装组件 yum install openstack-neutron-linuxbridge ebtable ...

  7. 2.hello rabbitmq

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-22 22:49:50 星期一 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  8. JavaScript中的对象引用和复制

    在JavaScript分为两种原始值和引用值类型,原始值之间的复制是值对值得复制,而引用类型则是引用对引用的复制: // 原始值的复制: let num1 = 1; let num2 = num1; ...

  9. 买车交税 All In One

    中国买车交税 All In One 消费税 增值税 车辆购置税 车船税 关税 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原 ...

  10. npm publish bug & solution

    npm publish bug & solution npm ERR! Unexpected token < in JSON at position 0 while parsing ne ...