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 )
);
现在向 Player 表插入一些数据:
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 |
+-----------+----------------+--------------+------------------+-------------------+