Not sure how to title my question... lol.
Below is what i am needing.
Values in my database look like the following: test_example-1, test_example-2, test_example-TD-1
The values can vary in length, "test_example-" is just an example, some of the values will have varying names.
When i perform a query to grab all the values that look like this "test_example-" i get everything, because i am using the wild card value this way "test_example-%". However, i do not want the "test_example-TD-1" in the result. I only want the NON-TD values. So, how can i get all the "test_example-" without getting stuff that has more than just a single digit integer after the "-".
A value in the database will not have anymore than a single digit number (0-9) after the "-". So basically i need a query that searches for values that would look like this "test_example-[0-9]".
How can i do that? If this is confusing and needs clarification, please let me know. Thanks!
Here is the exact query that i am doing to ease some confusion:
SELECT MAC, NAME FROM HOST WHERE NAME LIKE (SELECT CONCAT(LEFT(NAME, LENGTH(NAME)-1), "%") FROM HOST WHERE MAC="some mac address");
Here is the working result ( thanks to swati ):
SELECT MAC, NAME FROM HOST WHERE NAME REGEXP CONCAT(LEFT(NAME, LENGTH(NAME)-1), "[0-9]+") AND MAC="some mac address";
SELECT * FROM `foo` WHERE `value` REGEXP "test_example-[:digit:]+"
That should work for test_example-[0-9]. More on Character Classes and REGEXP
With your given query, it would be:
SELECT MAC, NAME FROM HOST WHERE NAME REGEXP CONCAT(LEFT(NAME, LENGTH(NAME)-1), "[0-9]+") AND MAC="some mac address"
Note there is an error in your posted query - you have two WHERE clauses.
where substring(field,locate('-',field)+1,1) in ('1','2','3','4','5','6','7','8','9','0')
You could try to use a regexp , though I don't know how efficient it will be. You might want to do it in code rather then in sql. but still:
SELECT * FROM `foo` WHERE `value` REGEXP "yourRegexp"
Just create any regexp that satisfies your needs.
您可以使用下划线字符作为单个字符的通配符。
"test_example-_"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.