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

 select 'user' as tablename, count(*) from user
union all select 'teacher' as tablename, count(*) from teacher
union all select 'person' as tablename, count(*) from person
union all select 'student' as tablename, count(*) from student
order by tablename

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

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

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

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

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

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

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

 select REPLACE(REPLACE(name, char(), ''), char(), '') as name from user where name like CONCAT("%",char(),"%")
union all select REPLACE(REPLACE(name, char(), ''), char(), '') as name from student where name like CONCAT("%",char(),"%")
union all select REPLACE(REPLACE(name, char(), ''), char(), '') as name from person where name like CONCAT("%",char(),"%")
union all select REPLACE(REPLACE(name, char(), ''), char(), '') as name from student where name like CONCAT("%",char(),"%");

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

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

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

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

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

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

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

 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。

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

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

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

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

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

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

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

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

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

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

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

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

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

待续......

常用Mysql或者PostGresql或者Greenplum的语句总结。的更多相关文章

  1. mysql迁移mpp数据库Greenplum

    1. 场景描述 因兄弟项目中mysql有点扛不住了,要做sql优化,但是业务有点小复杂,优化起来有点麻烦(sql嵌套有点多),便想着用Mpp数据库Greenplum测试下,看性能和复杂度怎么样,趟趟水 ...

  2. 常用SQL操作(MySQL或PostgreSQL)与相关数据库概念

    本文对常用数据库操作及相关基本概念进行总结:MySQL和PostgreSQL对SQL的支持有所不同,大部分SQL操作还是一样的. 选择要用的数据库(MySQL):use database_name; ...

  3. 【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

    一.WampServer2.2设置数据库默认编码(此部分转自http://www.cnsecer.com/5984.html) wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话 ...

  4. 常用MySQL语句整合

    常用MySQL语句整合 1. MySQL服务的配置和使用 修改MySQL管理员的口令:mysqladmin –u root password 密码字符串 如:mysqldmin –u root pas ...

  5. Mysql 和 Postgresql(PGSQL) 对比

    Mysql 和 Postgresql(PGSQL) 对比 转载自:http://www.oschina.net/question/96003_13994 PostgreSQL与MySQL比较 MySQ ...

  6. PostgreSQL和Greenplum、Npgsql

    PostgreSQL和Greenplum.Npgsql 想着要不要写,两个原因“懒”和“空”.其实懒和空也是有联系的,不是因为懒的写,而是因为对PostgreSQL和Npgsql的知识了解匮乏,也就懒 ...

  7. 对Oracle 、SQL Server、MySQL、PostgreSQL数据库优缺点分析

    对Oracle .SQL Server.MySQL.PostgreSQL数据库优缺点分析 Oracle Database Oracle Database,又名Oracle RDBMS,或简称Oracl ...

  8. SQLite vs MySQL vs PostgreSQL:关系型数据库比较

    自1970年埃德加·科德提出关系模型之后,关系型数据库便开始出现,经过了40多年的演化,如今的关系型数据库种类繁多,功能强大,使用广泛.面对如此之多的关系型数据库,我们应该如何权衡找出适合自己应用场景 ...

  9. 去 IOE,MySQL 完胜 PostgreSQL

    本文转载自: http://www.innomysql.net/article/15612.html (只作转载, 不代表本站和博主同意文中观点或证实文中信息) 前言 上周参加了2015年的中国数据库 ...

随机推荐

  1. AirBnB春招笔试题

    试题说明 笔试题只有一道,限时1小时. 模拟一个战争外交游戏,游戏中定义了三种操作: A city1 Hold : 军队A 占领了city1 A city1 Move city2 : 军队A从city ...

  2. Windows服务器【由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作】问题调查

    今天测试反应了一个问题,说接口返回的速度变慢了,并且返回的数据也不对.然后就找到了我o(╥﹏╥)o. 第一个反应就是查日志,不查不要紧,一查吓一跳,整个服务器上所有的站点都报错了.异常信息如下: Sy ...

  3. [WC2007]剪刀石头布(最大流)

    洛古 一句话题意:给定一张图,每两点之间有一条有向边或无向边,把所有无向边定向,使图中三元环个数尽量多 因为原图是一个完全图,假设图中任意三点都能构成三元环,那么途中三元环的个数为:\(\binom{ ...

  4. JDK动态代理(Proxy)的两种实现方式

    JDK自带的Proxy动态代理两种实现方式 前提条件:JDK Proxy必须实现对象接口 so,创建一个接口文件,一个实现接口对象,一个动态代理文件 接口文件:TargetInterface.java ...

  5. 为什么要使用50ohm阻抗?

    对于接收机我们期望同轴线的损耗越低越好 对于发射机同轴线的功率效率则是越大越好 所以在二者性能最优时阻抗并不相等 https://www.sohu.com/a/109536765_335274

  6. (五)qt资源文件

    // 规则: :+添加的前缀/+文件名 ui->actionSave_as->setIcon(QIcon(":/new/Image/face.png"));

  7. BZOJ3932 主席树

    https://www.lydsy.com/JudgeOnline/problem.php?id=3932 题意:给出一些带有等级的线段,求一点上前K小个等级线段的等级之和 询问是对于每一个点询问前K ...

  8. 解读Scrapy框架

    Scrapy框架基础:Twsited Scrapy内部基于事件循环的机制实现爬虫的并发.原来: url_list = ['http://www.baidu.com','http://www.baidu ...

  9. 金融量化分析【day110】:NumPy通用函数

    一.通用函数 能同时对数组中所有元素进行运算的函数 1.一元函数 1.sqrt 2.ceil 3.modf 4.isnan 5.abs 2.二元函数 1.maxinum 二.数学和统计方法 1.sum ...

  10. Caused by: java.lang.ClassNotFoundException: org.springframework.web.filter.FormContentFilter

    又是一个报错,我写代码真的是可以,所有的bug都会被我遇到,所有的问题我都能踩一遍,以前上学的时候同学就喜欢问我问题,因为他们遇到的问题,我早就遇到了......... 看看报错内容: 2019-04 ...