[英]Mysql - extract number from string
我在mysql表中有路徑字段,該字段存儲頁面的路徑。
它的頁面ID在末尾,例如33、31、121 ...
如何編寫查詢以僅獲取ID?
Path
-----
trending-architectures/architecture_detail_page/31
trending-architectures/architecture_detail_page/33
trending-architectures/architecture_detail_page/33
trending-architectures/architecture_detail_page/33
trending-architectures/architecture_detail_page/33
如果模式相同,則可以使用函數substring_index()
mysql> select substring_index('trending-architectures/architecture_detail_page/31','/',-1) as num;
+-----+
| num |
+-----+
| 31 |
+-----+
1 row in set (0.00 sec)
所以選擇語句將作為
select
substring_index(path,'/',-1) as number
from table_name
;
我可以想到兩種解決方案,第一種是利用explode使用“ /” deliemeter將字符串拆分成數組,然后在變量內設置數組的最后一個值(即頁面ID)
$path = explode('/', $path);
$page = end($path);
其他解決方案只是用數字替換數字之前的任何文本
$page = str_replace('trending-architectures/architecture_detail_page/', '', $path);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.