Database commonly used sentences and usage skills

Query data by date

select * from 表名 where 日期字段>to_date('2020-01-05','YYYY-MM-DD');
select * from 表名 where 日期字段>date'2020-01-05'
select * from 表名 where 日期字段>'2020-01-05'
--可查询出日期字段大于2020-01-05的数据

case when then else用法

select
    name,
    (case when score < 60 then '不及格'
        when score >= 60 and score < 80 then '及格'
        when score >= 80 then '优秀'
        when score is null then '缺席考试'
        else '异常' end) as grade
from
    table
--when后面跟字段的条件,then后面跟展示的结果,else后面跟不符合任何条件时展示的结果,as后面跟字段名称。
--以上查询的结果就是:查询出两个字段name和grade,grade字段的值是根据when后面的条件来展示
--如果score<60,那么grade的值为'不及格';如果score不符合when后的任何条件,那么grade的值为'异常'
--then后面直接跟字段也可以
case when 交易方向='buy' then 买方姓名 when 交易方向='sell' then 卖方姓名 else null end 本方交易员姓名  --交易方向、买方姓名、卖方姓名都是字段名
select
case when 交易方向='buy' then 买方姓名 when 交易方向='sell' then 卖方姓名 else null end 本方交易员姓名,
to_char(金额,‘fm9999990.00’),--将金额字段的格式转换为整数位最大七位,小数位保留两位
abs(手续费),--取手续费字段的绝对值
decode(币种,'1','USD','2','EUR','3','HKD',NULL)--币种字段为1时,显示USD;如果不等于1或2或3,则显示为空。
from 交易明细表

Millisecond representation method
Postgre millisecond representation: YYYYMMDD HH24MISS.MS
oracle millisecond representation: YYYYMMDD HH24MISS.FF3

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/109780571