MySQL 的 POSITION(~)
方法返回源字符串中子字符串第一次出現的位置。
用法
POSITION(substr IN str);
參數
1. substr
| string
我們試圖在 str
中查找子字符串。
2. str
| string
我們嘗試在其中查找子字符串的源字符串。
返回值
案子 |
返回值 |
---|---|
如果 |
|
如果 |
0 |
例子
考慮下表有關一些學生的信息:
student_id |
名稱 |
名字 |
day_enrolled |
年齡 |
用戶名 |
---|---|---|---|---|---|
1 |
Sky |
Towner |
2015-12-03 |
17 |
stowner1 |
2 |
Ben |
Davis |
2016-04-20 |
19 |
bdavis2 |
3 |
Travis |
Apple |
2018-08-14 |
18 |
tapple3 |
4 |
Arthur |
David |
2016-04-01 |
16 |
adavid4 |
5 |
Benjamin |
Town |
2014-01-01 |
17 |
btown5 |
可以使用此處的代碼創建上述示例表。
基本用法
要查找用戶名中第一次出現 'd'
的位置:
SELECT username, POSITION('d' IN username) AS 'Position of d'
FROM students;
+----------+---------------+
| username | Position of d |
+----------+---------------+
| stowner1 | 0 |
| bdavis2 | 2 |
| tapple3 | 0 |
| adavid4 | 2 |
| btown5 | 0 |
+----------+---------------+
請注意,MySQL 中的位置計數從 1 開始,與其他一些從 0 開始的編程語言不同。
區分大小寫
搜索不區分大小寫:
SELECT username, POSITION('D' IN username) AS 'Position of D'
FROM students;
+----------+---------------+
| username | Position of D |
+----------+---------------+
| stowner1 | 0 |
| bdavis2 | 2 |
| tapple3 | 0 |
| adavid4 | 2 |
| btown5 | 0 |
+----------+---------------+
即使使用 'D'
(大寫字母)作為子字符串,我們仍然可以在用戶名 bdavis
和 adavid4
中找到匹配項。
相關用法
- MySQL POSITION()用法及代碼示例
- MySQL POWER()用法及代碼示例
- MySQL POWER方法用法及代碼示例
- MySQL POW()用法及代碼示例
- MySQL POW方法用法及代碼示例
- MySQL PI()用法及代碼示例
- MySQL PERIOD_DIFF()用法及代碼示例
- MySQL PERIOD_ADD方法用法及代碼示例
- MySQL PI方法用法及代碼示例
- MySQL PERIOD_DIFF方法用法及代碼示例
- MySQL PASSWORD用法及代碼示例
- MySQL PERIOD_ADD()用法及代碼示例
- MySQL ROUND()用法及代碼示例
- MySQL REPEAT()用法及代碼示例
- MySQL LEAD() and LAG()用法及代碼示例
- MySQL IS_IPV4()用法及代碼示例
- MySQL RADIANS方法用法及代碼示例
- MySQL VARIANCE方法用法及代碼示例
- MySQL WEEK()用法及代碼示例
- MySQL TIME_FORMAT方法用法及代碼示例
- MySQL CURTIME()用法及代碼示例
- MySQL weekofyear()用法及代碼示例
- MySQL Convert()用法及代碼示例
- MySQL IS NOT用法及代碼示例
- MySQL FROM_BASE64()用法及代碼示例
注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 MySQL | POSITION method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。