【sql(mysql + oracle)】

【Mysql】 正則表達式

查看owner欄位中每一個值的出現次數。 sql

select distinct owner as uowner , count(*)  as count 測試

from t_l0

group by uowner spa

order by count DESC 字符串

【Mysql】模式匹配 REGEXP string

SELECT * FROM `t_l1` WHERE `file` REGEXP "custom/[_0-9a-zA-Z]+/modem/" it

【Mysql】substring()截取字符串 和 concat()鏈接字符串 擴展

SELECT file as afile,owner,context FROM `t_l0_mp1` WHERE context=(select context from t_l0_flag2 where file = substring(afile,19)) date

【Mysql】Left join ,right join,join(=inner join) file

A 右鏈接B,結果顯示A的所有,反之,A左鏈接B,結果會顯示B的所有

Select A.file,B.file
From t_l0_mp1 AS A 
Left Join t_l0_flag2 AS B
On substring(A.file,20)=B.file and A.owner=B.owner and A.context=B.context 

Select A.file,B.file
From t_l0_mp1 AS A 
Join t_l0_flag2 AS B
On substring(A.file,20)=B.file and A.owner=B.owner and A.context=B.context 
等同於

Select A.file,B.file
From t_l0_mp1 AS A,t_l0_flag2 AS B
WHERE substring(A.file,20)=B.file and A.owner=B.owner and A.context=B.context 

【Mysql】聯表更新 update + join

update t_l0_mp8 as A Join t_l0_flag2 as B
ON substring(A.file,20)=B.file 
and A.owner = B.owner 
and A.context = B.context
SET A.flag=2

【Mysql】取一個表數據插入另外一個表

INSERT IGNORE INTO `t_flag2`( `file`, `owner`, `context`) SELECT concat(substring_index(concat('/alps',substring_index(file,'/alps',-1)),'#',1),'#'),owner,context from t_l0_mp8 where flag=2

避免重複插入的方法 :ignore,Replace,ON DUPLICATE KEY UPDATE

【Mysql】

DELETE FROM sync_flag2 WHERE EXISTS (SELECT id from t_l0_mp2 AS B where (B.flag=4 or B.flag=1) and concat(substring_index(concat('/alps',substring_index(B.file,'/alps',-1)),'#',1),'#')=file and B.owner=owner and B.context=context)

DELETE FROM sync_flag2 AS A  WHERE EXISTS (SELECT id from t_l0_mp2 AS B where (B.flag=4 or B.flag=1) and concat(substring_index(concat('/alps',substring_index(B.file,'/alps',-1)),'#',1),'#')=A.file and B.owner=A.owner and B.context=A.context)DELETE FROM sync_flag2 AS A格式不能用在delete當中】

【Mysql】

使用「_」匹配任何單個字符,而「%」匹配任意數目字符(包括零個字符)。在 MySQL中,SQL的模式缺省是忽略大小寫的。注意在你使用SQL模式時,你不能使用=或!=;而使用LIKE或NOT LIKE比較操做符。

由MySQL提供的模式匹配的其餘類型是使用擴展正則表達式。當你對這類模式進行匹配測試時,使用REGEXP和NOT REGEXP操做符(或RLIKE和NOT RLIKE,它們是同義詞)。

「.」匹配任何單個的字符。

一個字符類「[...]」匹配在方括號內的任何字符。例如,「[abc]」匹配「a」、「b」或「c」。爲了命名字符的一個範圍,使用一個「-」。

「[a-z]」匹配任何小寫字母,而「[0-9]」匹配任何數字。

「 * 」匹配零個或多個在它前面的東西。例如,「x*」匹配任何數量的「x」字符,「[0-9]*」匹配的任何數量的數字,而「.*」匹配任何數量的任何東西。

正則表達式是區分大小寫的,可是若是你但願,你能使用一個字符類匹配兩種寫法。例如,「[aA]」匹配小寫或大寫的「a」而「[a-zA-Z]」匹配兩種寫法的任何字母。

若是它出如今被測試值的任何地方,模式就匹配(只要他們匹配整個值,SQL模式匹配)。

爲了定位一個模式以便它必須匹配被測試值的開始或結尾,在模式開始處使用「^」或在模式的結尾用「$」。

爲了說明擴展正則表達式如何工做,上面所示的LIKE查詢在下面使用REGEXP重寫:

爲了找出以「b」開頭的名字,使用「^」匹配名字的開始而且「[bB]」匹配小寫或大寫的「b」:

爲了找出以「fy」結尾的名字,使用「$」匹配名字的結尾:

【Oracle】

round(to_number(sysdate-8/24-T1.submit_date))>=7

sysdate 比北京時間早8小時

相關文章
相關標籤/搜索