mysql详细查询字符串,如通配符

编程入门 行业动态 更新时间:2024-10-25 07:26:18
本文介绍了mysql详细查询字符串,如通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

不确定如何为我的问题加标题...大声笑.

Not sure how to title my question... lol.

下面是我所需要的.

我数据库中的值如下所示:test_example-1,test_example-2,test_example-TD-1

Values in my database look like the following: test_example-1, test_example-2, test_example-TD-1

值的长度可以变化,"test_example-"只是一个例子,其中一些值的名称会有所不同.

The values can vary in length, "test_example-" is just an example, some of the values will have varying names.

当我执行查询以获取所有看起来像"test_example-"的值时,我得到了所有内容,因为我以这种方式使用通配符"test_example-%".但是,我不希望结果中出现"test_example-TD-1".我只想要NON-TD值.因此,如何获得所有的"test_example-"而又不会得到在-"之后仅具有一个整数整数的东西.

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 "-".

数据库中的值在-"之后的位数不得超过一个数字(0-9).因此,基本上我需要一个查询来查找看起来像"test_example- [0-9]"的值的查询.

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");

这是工作结果(感谢swati):

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:]+"

这应该适用于test_example- [0-9].有关字符类和REGEXP

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"

请注意,您发布的查询中有一个错误-您有两个WHERE子句.

Note there is an error in your posted query - you have two WHERE clauses.

更多推荐

mysql详细查询字符串,如通配符

本文发布于:2023-10-24 14:53:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串   详细   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!