常用Mysql或者PostGresql或者Greenplum的语句总结。

1、使用mysql的union all可以同时查询出所有自己想要查询数据表的数据量。

1 select 'user' as tablename, count(*) from user
2 union all select 'teacher' as tablename, count(*) from teacher
3 union all select 'person' as tablename, count(*) from person
4 union all select 'student' as tablename, count(*) from student
5 order by tablename

2、使用mysql的union all可以同时查询出所有自己想要查询数据表的数据量。添加上限制条件进行查询。

1 select 'user' as tablename, count(*) from user                                         where update_time>'2018-10-09'
2 union all select 'teacher' as tablename, count(*) from teacher                         where update_time>'2018-10-09'
3 union all select 'person' as tablename, count(*) from person                           where update_time>'2018-10-09'
4 union all select 'student' as tablename, count(*) from student                         where update_time>'2018-10-09'
5 order by tablename   

3、使用Postgresql或者Greenplum的union all可以同时查询出所有自己想要查询数据表的数据量。添加上限制条件进行查询。

1 select 'user' as tablename, count(*) from user                                          where update_time>to_date('2018-10-09 01', 'yyyy-mm-dd hh24')
2 union all select 'teacher' as tablename, count(*) from teacher                          where update_time>to_date('2018-10-09 01', 'yyyy-mm-dd hh24')
3 union all select 'person' as tablename, count(*) from person                            where update_time>to_date('2018-10-09 01', 'yyyy-mm-dd hh24')
4 union all select 'student' as tablename, count(*) from student                          where update_time>to_date('2018-10-09 01', 'yyyy-mm-dd hh24')
5 order by tablename   

4、在Mysql数据库中,如果某个字段是换行的,如何去掉换行的字段,然后正常查询出来。
    注意:char(10)换行键、char(13)回车键。
    4.1、查询出多个数据表某条记录可能含有换行符的记录。
        CONCAT()函数用于将多个字符串连接成一个字符串。

1 select * from user where name like CONCAT("%",char(13),"%")
2 union all select * from teacher  where name like CONCAT("%",char(13),"%")
3 union all select * from person  where name like CONCAT("%",char(13),"%")
4 union all select * from student  where name like CONCAT("%",char(13),"%");

    4.1、然后将换行和回车进行替换,将换行和回车换成''。这样做就将回车和换行替换完成。
        replace(object,search,replace),把object中出现search的全部替换为replace。

1 select REPLACE(REPLACE(name, char(10), ''), char(13), '') as name from user where name like CONCAT("%",char(13),"%")
2 union all select REPLACE(REPLACE(name, char(10), ''), char(13), '') as name from student where name like CONCAT("%",char(13),"%")
3 union all select REPLACE(REPLACE(name, char(10), ''), char(13), '') as name from person where name like CONCAT("%",char(13),"%")
4 union all select REPLACE(REPLACE(name, char(10), ''), char(13), '') as name from student where name like CONCAT("%",char(13),"%");

    4.3、可以将回车符和换行符转换为特殊的字符。
        -- 将char(10)换行键,char(13)回车键换成@#r;和@#n;

1  select REPLACE(REPLACE(name, char(10), '@#r;'), char(13), '@#n;') as name from user where name like CONCAT("%",char(13),"%")

        如果需要有需要,可以将特殊的字符再转换为回车符和换行符。
        -- 将@#r;和@#换成nchar(10)换行键,char(13)回车键;

1  select REPLACE(REPLACE(name, '@#r;', char(10)), '@#n;', char(13)) as name from user where name like CONCAT("%",char(13),"%")

5、    查询出最大时间,可以根据这个来进行批次插入数据和批次导出数据。
    5.1、Postgresql和Greenplum的用法:
        COALESCE()的用法,如果第一个参数不为null,咋返回第一个参数,否则返回第二个参数。

1 select '数据表名称' as table_name, 5 as part, COALESCE(max(update_time), now()) as next_time from schema.数据表名称

5.2、Mysql的用法:
        ifnull()的用法,如果第一个参数不为null,咋返回第一个参数,否则返回第二个参数。

1 select 'user' as table_name,ifnull(max(update_time),now()) as update_time from user;

6、MYSQL或者Postgresql和Greenplum的Case...When的用法基本相同。
    6.1、Case expr when v1 then r1 when v2 then r2 else rn end。该函数表示,如果expr值等于某个vn,则返回对应位置then后面的结果,如果所有值都不相等,则返回else后面的rn。

1  select case 2 when 1 then 'one' when 2 then 'two' else 'more' end;

    6.2、case when v1 then r1 when v2 then r2 else rn end。该函数表示,某个vn为true的时候,则返回对应位置then后面的结果,如果所有值都不相等,则返回else后面的rn。

1  select case when 1 < 0 then 'true' when 1 > 0 then 'false' else 'more' end;

7、查询最大批次号,如果不存在根据规则生成一个批次号。
    Postgresql和Greenplum使用to_number()函数来转换成整数、to_char()将数字转为字符串。mysql无此函数。
    完整的例子如下所示:

1 select to_char(to_number(COALESCE(max("Cd_batch"), to_char(now(), 'yyyyMMdd')||'00000'), '9999999999999') + 1, '9999999999999') from schema.数据表名称 where "TableName"='数据表名称' and "Cd_source"='数据来源'

    步骤一:获取最大的批次号:

1 select max("Cd_batch") from schema.数据表名称 where "TableName"='数据表名称' and "Cd_source"='数据来源'

    步骤二:判断如果最大批次号如果为null,根据规则生成一个批次号。

1 select COALESCE(max("Cd_batch"), to_char(now(), 'yyyyMMdd')||'00000') from schema.数据表名称 where "TableName"='数据表名称' and "Cd_source"='数据来源'

    步骤三:将生成的字符串转换为数值类型numeric的,长度为第二个参数的长度,并且批次号加1。这样下发的数据的批次号就是叠加后的批次号。

1  select to_number(COALESCE(max("Cd_batch"), to_char(now(), 'yyyyMMdd')||'00000'), '9999999999999') + 1 from schema.数据表名称 where "TableName"='数据表名称' and "Cd_source"='数据来源'

    步骤四:将生成的数值numeric类型转换为字符串类型的。

1 select to_char(to_number(COALESCE(max("Cd_batch"), to_char(now(), 'yyyyMMdd')||'00000'), '9999999999999') + 1, '9999999999999') from schema.数据表名称 where "TableName"='数据表名称' and "Cd_source"='数据来源'

 

 

 

待续......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值