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 >= #{startTime} </if> <if test="null != endTime"> and create_time <= #{endTime} </if>) or ( 1=1 <if test="null != startTime"> and update_time >= #{startTime} </if> <if test="null != endTime"> and update_time <= #{endTime} </if>) ) ORDER BY update_time desc, create_time desc </select>
when可以使用多次
select case when t1.line_type = 1 then t1.complete_qty when t1.line_type = 2 then t1.import_num end AS "num", from t_traceability_product t1;
力扣中的题目:
给定一个 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
如果不适用聚合函数,则结果如下:
© 著作权归作者所有
相关热门文章
发表评论