當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


MySQL ORD()用法及代碼示例


MySQL中的ORD()函數用於查找字符串中最左邊的字符的代碼。如果最左邊的字符不是多字節字符,則返回ASCII值。並且如果字符串str的最左邊的字符是一個多字節字符,那麽ORD返回該字符的代碼,該代碼是使用以下公式根據其組成字節的數值計算得出的:

(第一字節代碼)+(第二字節代碼* 256)+(第三字節代碼* 256^2)……

用法:

ORD( str )

參數:該函數接受上麵提到並在下麵描述的一個參數:

  • str:給定的字符串,其最左邊的字符代碼將被查找。

返回值:它返回字符串中最左邊的字符的代碼。



示例1:將ORD()函數應用於單個字符。

SELECT ORD('S') as Find_Code;

輸出:

Find_Code
83

示例2:將ORD()函數應用於字符串。

SELECT ORD('geeksforgeeks') as Find_Code;

輸出:

Find_Code
103

示例3:將ORD()函數應用於號碼。

SELECT ORD(100) as Find_Code;

輸出:

Find_Code
49

示例4:
ORD函數還可用於查找列數據最左邊字符的代碼。為了演示創建一個名為的表。

播放器

CREATE TABLE Player(

    Player_id INT AUTO_INCREMENT,  
    Player_name VARCHAR(100) NOT NULL,
    Playing_team VARCHAR(20) NOT NULL,
    PRIMARY KEY(Player_id )

);

現在將一些數據插入播放器表:

INSERT INTO  
    Player(Player_name ,Playing_team)

VALUES
    ('Virat Kohli' , 'RCB' ) ,
    ('Rohit Sharma' , 'MI' ) ,
    ('Dinesh Karthik', 'KKR'  ) ,
    ('Shreyash Iyer' , 'DC'  ) ,
    ('David Warner' , 'SRH' ) ,
    ('Steve Smith' , 'RR'  ) ,
    ('Andre Russell' , 'KKR' ) ,
    ('Jasprit Bumrah' , 'MI' ) ,
    ('Risabh Panth', 'DC'  ) ;

因此,播放器表為:

mysql> SELECT * FROM Player;
+-----------+----------------+--------------+
| Player_id | Player_name    | Playing_team |
+-----------+----------------+--------------+
|         1 | Virat Kohli    | RCB          |
|         2 | Rohit Sharma   | MI           |
|         3 | Dinesh Karthik | KKR          |
|         4 | Shreyash Iyer  | DC           |
|         5 | David Warner   | SRH          |
|         6 | Steve Smith    | RR           |
|         7 | Andre Russell  | KKR          |
|         8 | Jasprit Bumrah | MI           |
|         9 | Risabh Panth   | DC           |
+-----------+----------------+--------------+

現在,我們將應用ORD函數來查找列Player_name和Playing_team的最左邊字符的代碼。

Select 
    * ,
    ORD(Player_name) ,   
    ORD(Playing_team)  
FROM Player;

輸出:

+-----------+----------------+--------------+------------------+-------------------+
| Player_id | Player_name    | Playing_team | ORD(Player_name) | ORD(Playing_team) |
+-----------+----------------+--------------+------------------+-------------------+
|         1 | Virat Kohli    | RCB          |               86 |                82 |
|         2 | Rohit Sharma   | MI           |               82 |                77 |
|         3 | Dinesh Karthik | KKR          |               68 |                75 |
|         4 | Shreyash Iyer  | DC           |               83 |                68 |
|         5 | David Warner   | SRH          |               68 |                83 |
|         6 | Steve Smith    | RR           |               83 |                82 |
|         7 | Andre Russell  | KKR          |               65 |                75 |
|         8 | Jasprit Bumrah | MI           |               74 |                77 |
|         9 | Risabh Panth   | DC           |               82 |                68 |
+-----------+----------------+--------------+------------------+-------------------+

相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 ORD() Function in MySQL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。