首頁 資料庫 mysql教程 postgresql中常用小语法

postgresql中常用小语法

Jun 07, 2016 pm 02:58 PM
postgresql 常用 文法

postgresql中常用小语法 1. PG中 类型转换 有时候在postgresql中需要对值的类型进行转换,pg中提供的方法 example : select 33:: integer example2: select case(33 as integer) 2. pg中的行号 (类似于oracle中的 rownum) example : select row_number() o

postgresql中常用小语法

 

1. PG中 类型转换

 

    有时候在postgresql中需要对值的类型进行转换,pg中提供的方法

 

 example :  select '33':: integer

 

 example2: select case('33' as integer)

 

2. pg中的行号 (类似于oracle中的 rownum)

 

  example : select row_number() over() , * from XXXX

 

3. pg 中查询中的列转数组

 

example :  select array_agg(AAAAA)  FROM XXXXX

 

4. pg 中字符串函数 :

 

函数:string || string 

说明:String concatenation 字符串连接操作

例子:'Post' || 'greSQL' = PostgreSQL

 

函数:string || non-string or non-string || string

说明:String concatenation with one non-string input 字符串与非字符串类型进行连接操作

例子:'Value: ' || 42 = Value: 42

 

函数:bit_length(string)

说明:Number of bits in string 计算字符串的位数

例子:bit_length('jose') = 32

 

函数:char_length(string) or character_length(string)

说明:Number of characters in string 计算字符串中字符个数

例子:char_length('jose') = 4

 

函数:lower(string)

说明:Convert string to lower case 转换字符串为小写

例子:bit_length('jose') = 32

 

函数:octet_length(string)

说明:Number of bytes in string 计算字符串的字节数

例子:octet_length('jose') = 4

 

函数:overlay(string placing string from int [for int])

说明:Replace substring 替换字符串中任意长度的子字串为新字符串

例子:overlay('Txxxxas' placing 'hom' from 2 for 4) = 4

 

函数:position(substring in string)

说明:Location of specified substring 子串在一字符串中的位置

例子:position('om' in 'Thomas') = 3

 

函数:substring(string [from int] [for int])

说明:Extract substring 截取任意长度的子字符串

例子:substring('Thomas' from 2 for 3) = hom

 

函数:substring(string from pattern)

说明:Extract substring matching POSIX regular expression. See Section 9.7 for more information on pattern matching. 利用正则表达式对一字符串进行任意长度的字串的截取

例子:substring('Thomas' from '...$') = mas

 

函数:substring(string from pattern for escape)

说明:Extract substring matching SQL regular expression. See Section 9.7 for more information on pattern matching. 利于正则表达式对某类字符进行删除,以得到子字符串

例子:trim(both 'x' from 'xTomxx') = Tom

 

函数:trim([leading | trailing | both] [characters] from string)

说明:Remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string 去除尽可能长开始,结束或者两边的某类字符,默认为去除空白字符,当然可以自己指定,可同时指定多个要删除的字符串

例子:trim(both 'x' from 'xTomxx') = Tom

 

函数:upper(string)

说明:Convert string to uppercase 将字符串转换为大写

例子:upper('tom') = TOM

 

函数:ascii(string)

说明:ASCII code of the first character of the argument. For UTF8 returns the Unicode code point of the character. For other multibyte encodings. the argument must be a strictly ASCII character. 得到某一个字符的Assii值

例子:ascii('x') = 120

 

函数:btrim(string text [, characters text])

说明:Remove the longest string consisting only of characters in characters (a space by default) from the start and end of string 去除字符串两边的所有指定的字符,可同时指定多个字符

例子:btrim('xyxtrimyyx', 'xy') = trim

 

 

函数:chr(int)

说明:Character with the given code. For UTF8 the argument is treated as a Unicode code point. For other multibyte encodings the argument must designate a strictly ASCII character. The NULL (0) character is not allowed because text data types cannot store such bytes. 得到某ACSII值对应的字符

例子:chr(65) = A

 

 

函数:convert(string bytea, src_encoding name, dest_encoding name)

说明:Convert string to dest_encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. Conversions can be defined by CREATE CONVERSION. Also there are some predefined conversions. See Table 9-7 for available conversions. 转换字符串编码,指定源编码与目标编码

例子:convert('text_in_utf8', 'UTF8', 'LATIN1') = text_in_utf8 represented in ISO 8859-1 encoding

 

 

函数:convert_from(string bytea, src_encoding name)

说明:Convert string to the database encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. 转换字符串编码,自己要指定源编码,目标编码默认为数据库指定编码,

例子:convert_from('text_in_utf8', 'UTF8') = text_in_utf8 represented in the current database encoding

 

 

函数:convert_to(string text, dest_encoding name)

说明:Convert string to dest_encoding.转换字符串编码,源编码默认为数据库指定编码,自己要指定目标编码,

例子:convert_to('some text', 'UTF8') = some text represented in the UTF8 encoding

 

 

函数:decode(string text, type text)

说明:Decode binary data from string previously encoded with encode. Parameter type is same as in encode. 对字符串按指定的类型进行解码

例子:decode('MTIzAAE=', 'base64') = 123\000\001

 

 

函数:encode(data bytea, type text)

说明:Encode binary data to different representation. Supported types are: base64, hex, escape. Escape merely outputs null bytes as \000 and doubles backslashes. 与decode相反,对字符串按指定类型进行编码

例子:encode(E'123\\000\\001', 'base64') = MTIzAAE=

 

 

函数:initcap(string)

说明:Convert the first letter of each word to uppercase and the rest to lowercase. Words are sequences of alphanumeric characters separated by non-alphanumeric characters. 将字符串所有的单词进行格式化,首字母大写,其它为小写

例子:initcap('hi THOMAS') = Hi Thomas

 

 

函数:length(string)

说明:Number of characters in string 讲算字符串长度

例子:length('jose') = 4

 

 

函数:length(stringbytea, encoding name )

说明:Number of characters in string in the given encoding. The string must be valid in this encoding. 计算字符串长度,指定字符串使用的编码

例子:length('jose', 'UTF8') = 4

 

 

函数:lpad(string text, length int [, fill text])

说明:Fill up the string to length length by prepending the characters fill (a space by default). If the string is already longer than length then it is truncated (on the right). 对字符串左边进行某类字符自动填充,即不足某一长度,则在左边自动补上指定的字符串,直至达到指定长度,可同时指定多个自动填充的字符

例子:lpad('hi', 5, 'xy') = xyxhi

 

 

函数:ltrim(string text [, characters text])

说明:Remove the longest string containing only characters from characters (a space by default) from the start of string 删除字符串左边某一些的字符,可以时指定多个要删除的字符

例子:trim

 

 

函数:md5(string)

说明:Calculates the MD5 hash of string, returning the result in hexadecimal 将字符串进行md5编码

例子:md5('abc') = 900150983cd24fb0 d6963f7d28e17f72

 

 

函数:pg_client_encoding()

说明:Current client encoding name 得到pg客户端编码

例子:pg_client_encoding() = SQL_ASCII

 

 

函数:quote_ident(string text)

说明:Return the given string suitably quoted to be used as an identifier in an SQL statement string. Quotes are added only if necessary (i.e., if the string contains non-identifier characters or would be case-folded). Embedded quotes are properly doubled. 对某一字符串加上两引号

例子:quote_ident('Foo bar') = "Foo bar"

 

 

函数:quote_literal(string text)

说明:Return the given string suitably quoted to be used as a string literal in an SQL statement string. Embedded single-quotes and backslashes are properly doubled. 对字符串里两边加上单引号,如果字符串里面出现sql编码的单个单引号,则会被表达成两个单引号

例子:quote_literal('O\'Reilly') = 'O''Reilly'

 

 

函数:quote_literal(value anyelement)

说明:Coerce the given value to text and then quote it as a literal. Embedded single-quotes and backslashes are properly doubled. 将一数值转换为字符串,并为其两边加上单引号,如果数值中间出现了单引号,也会被表示成两个单引号

例子:quote_literal(42.5) = '42.5'

 

 

函数:regexp_matches(string text, pattern text [, flags text])

说明:Return all captured substrings resulting from matching a POSIX regular expression against the string. See Section 9.7.3 for more information. 对字符串按正则表达式进行匹配,如果存在则会在结果数组中表示出来

例子:regexp_matches('foobarbequebaz', '(bar)(beque)') = {bar,beque}

 

 

函数:regexp_replace(string text, pattern text, replacement text [, flags text])

说明:Replace substring(s) matching a POSIX regular expression. See Section 9.7.3 for more information. 利用正则表达式对字符串进行替换

例子:regexp_replace('Thomas', '.[mN]a.', 'M') = ThM

 

 

函数:regexp_split_to_array(string text, pattern text [, flags text ])

说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正则表达式将字符串分割成数组

例子:regexp_split_to_array('hello world', E'\\s+') = {hello,world}

 

 

函数:regexp_split_to_table(string text, pattern text [, flags text])

说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正则表达式将字符串分割成表格

例子:regexp_split_to_table('hello world', E'\\s+') =

hello

world

(2 rows)

 

 

函数:repeat(string text, number int)

说明:Repeat string the specified number of times 重复字符串一指定次数

例子:repeat('Pg', 4) = PgPgPgPg

 

 

函数:replace(string text, from text, to text)

说明:Replace all occurrences in string of substring from with substring to 将字符的某一子串替换成另一子串

例子:('abcdefabcdef', 'cd', 'XX') = abXXefabXXef

 

 

函数:rpad(string text, length int [, fill text])

说明:Fill up the string to length length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. 对字符串进行填充,填充内容为指定的字符串

例子:rpad('hi', 5, 'xy') = hixyx

 

 

函数:rtrim(string text [, characters text])

说明:Remove the longest string containing only characters from characters (a space by default) from the end of string

去除字符串右边指定的字符

例子:rtrim('trimxxxx', 'x') = trim

 

 

函数:split_part(string text, delimiter text, field int)

说明:Split string on delimiter and return the given field (counting from one)  对字符串按指定子串进行分割,并返回指定的数值位置的值

例子:split_part(mailto:'abc~@~def~@~ghi', mailto:'~@~', 2) = def

 

函数:strpos(string, substring)

说明:Location of specified substring (same as position(substring in string), but note the reversed argument order) 指定字符串在目标字符串的位置

例子:strpos('high', 'ig') = 2

 

函数:substr(string, from [, count])

说明:Extract substring (same as substring(string from from for count)) 截取子串

例子:substr('alphabet', 3, 2) = ph

 

函数:to_ascii(string text [, encoding text])

说明:Convert string to ASCII from another encoding (only supports conversion from LATIN1, LATIN2, LATIN9, and WIN1250 encodings) 将字符串转换成ascii编码字符串

例子:to_ascii('Karel') = Karel

 

函数:to_hex(number int or bigint)

说明:Convert number to its equivalent hexadecimal representation  对数值进行十六进制编码

例子:to_hex(2147483647) = 7fffffff

 

函数:translate(string text, from text, to text)

说明:Any character in string that matches a character in the from set is replaced by the corresponding character in the to set 将字符串中某些匹配的字符替换成指定字符串,目标字符与源字符都可以同时指定多个

例子:translate('12345', '14', 'ax') = a23x5

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MySQL和PostgreSQL:在Web開發中的最佳實踐 MySQL和PostgreSQL:在Web開發中的最佳實踐 Jul 14, 2023 pm 02:34 PM

MySQL和PostgreSQL:在Web開發中的最佳實務引言:在現代的Web開發領域中,資料庫是不可或缺的組成部分。在選擇資料庫時,常見的選擇是MySQL和PostgreSQL。本文將介紹在Web開發中使用MySQL和PostgreSQL的最佳實踐,並提供一些程式碼範例。一、適用場景MySQL適用於大多數Web應用程序,特別是那些需要高性能、可擴展性和易於使

MySQL與PostgreSQL:效能對比與最佳化技巧 MySQL與PostgreSQL:效能對比與最佳化技巧 Jul 13, 2023 pm 03:33 PM

MySQL與PostgreSQL:效能比較與最佳化技巧在開發web應用程式時,資料庫是不可或缺的組成部分。而在選擇資料庫管理系統時,MySQL和PostgreSQL是兩個常見的選擇。他們都是開源的關係型資料庫管理系統(RDBMS),但在效能和最佳化方面有一些不同之處。本文將比較MySQL和PostgreSQL的效能,並提供一些最佳化技巧。性能對比在比較兩個資料庫管

學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作 學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作 Jul 31, 2023 pm 12:54 PM

學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作在現代的軟體開發中,資料庫是不可或缺的一部分。 Go語言作為一門強大的程式語言,提供了豐富的資料庫操作函數和工具包,可以輕鬆實現資料庫的增刪改查操作。本文將介紹如何學習Go語言中的資料庫函數,並使用PostgreSQL資料庫進行實際的操作。第一步:安裝資料庫驅動程式在Go語言中,每個資料庫

在Go語言中使用PostgreSQL:完整指南 在Go語言中使用PostgreSQL:完整指南 Jun 18, 2023 am 09:28 AM

Go語言是一種快速、有效率的程式語言,適合建立Web服務和後端應用程式。而PostgreSQL是一個開源的關聯式資料庫管理系統,承諾提供更高的可靠性、可擴展性和資料安全性。在本文中,我們將深入探討如何在Go語言中使用PostgreSQL,並提供一些實用的程式碼範例和技巧。安裝和設定PostgreSQL首先,我們需要安裝和設定PostgreSQL。可以在官方網

MySQL與PostgreSQL:資料安全與備份策略 MySQL與PostgreSQL:資料安全與備份策略 Jul 13, 2023 pm 03:31 PM

MySQL與PostgreSQL:資料安全與備份策略引言:在現代社會中,資料成為了企業和個人生活中不可或缺的一部分。對於資料庫管理系統來說,資料安全與備份策略是至關重要的,既能保護資料免受遺失或損壞,也能確保恢復資料的可靠性和完整性。本文將重點放在MySQL和PostgreSQL兩種主流關係型資料庫系統的資料安全性和備份策略。一、資料安全性方面:(一)用戶權

學習canvas框架 詳解常用的canvas框架 學習canvas框架 詳解常用的canvas框架 Jan 17, 2024 am 11:03 AM

探索Canvas框架:了解常用的Canvas框架有哪些,需要具體程式碼範例引言:Canvas是HTML5中提供的一個繪圖API,透過它我們可以實現豐富的圖形和動畫效果。為了提高繪圖的效率和便利性,許多開發者開發了不同的Canvas框架。本文將介紹一些常用的Canvas框架,並提供具體程式碼範例,以幫助讀者更深入地了解這些框架的使用方法。一、EaselJS框架Ea

如何在PHP程式設計中使用PostgreSQL資料庫? 如何在PHP程式設計中使用PostgreSQL資料庫? Jun 12, 2023 am 09:27 AM

隨著資料庫技術的發展,資料庫管理系統也呈現出多種多樣的選擇,開發人員可以根據自己的需求和喜好選擇最適合自己的資料庫。而PostgreSQL作為一種先進的開源關係型資料庫系統,越來越受到開發人員的關注與使用。那麼,在PHP程式設計中如何使用PostgreSQL資料庫呢?一、安裝和設定PostgreSQL資料庫在使用PostgreSQL之前,需要先安裝並設定它。首先

lambda 表達式的語法和結構有什麼特色? lambda 表達式的語法和結構有什麼特色? Apr 25, 2024 pm 01:12 PM

Lambda表達式是無名稱的匿名函數,其語法為:(parameter_list)->expression。它們具有匿名性、多樣性、柯里化和閉包等特徵。在實際應用中,Lambda表達式可用於簡潔地定義函數,如求和函數sum_lambda=lambdax,y:x+y,並透過map()函數應用於列表來進行求和操作。

See all articles