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"='數據來源'
待續......