0

I have path field in mysql table which stors the path of the page.

It is having page id at the end like 33, 31, 121 ...

How can I write a query to get only the ids?

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
6
  • In PHP you can do it using preg_match_all ------------------------ preg_match_all('!\d+!', $str, $matches); print_r($matches); or else you can even use preg_match_all("/[^0-9]/","",$str,$matches); print_r($matches); Commented Mar 6, 2015 at 11:12
  • I have to do this in SELECT statement of mysql. Commented Mar 6, 2015 at 11:17
  • All the strings are of same pattern ? i.e. 2 / and the last value after / is a number ? Commented Mar 6, 2015 at 11:21
  • i cannot find a single query solution for this.. stackoverflow.com/questions/978147/… See this link.. It may help you... Commented Mar 6, 2015 at 11:21
  • and can i know 1 thing... Using PHP you can do it easily in a single step, right.. Why do you want to use MySQL for this ???? Commented Mar 6, 2015 at 11:25

2 Answers 2

2

If the pattern is same, you can use the function substring_index()

mysql> select substring_index('trending-architectures/architecture_detail_page/31','/',-1) as num;
+-----+
| num |
+-----+
| 31  |
+-----+
1 row in set (0.00 sec)

So the select statement would go as

select
substring_index(path,'/',-1) as number 
from table_name
;
Sign up to request clarification or add additional context in comments.

2 Comments

Hii @Abhik Chakraborty... i think he even have three numbers at the end (like 121, 122, ....).. Will this query work in that case ??
If the pattern is same it will get any number from the end.
0

I can think of two solutions, the first one utilizes explode to split the string into an array using a "/" deliemeter and then it sets the last value of the array (which is the page id) inside a variable

$path = explode('/', $path);
$page = end($path);

Other solution just replaces whatever text is before the number with nothing

$page = str_replace('trending-architectures/architecture_detail_page/', '', $path);

1 Comment

In php I can. I have to do this in SELECT statement of mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.