不使用正则表达式对字母和数字排列进行 SQL 匹配
是否可以为 MySQL 编写一条 SQL 语句,在不使用 REGEXP 的情况下查找与特定的字母/数字排列匹配的所有行?
即 SELECT myCol FROM myTable WHERE myCol='{number}{number}{letter}'
应该返回 12X 和 34Y,但不是 123X 或 34YY
[我之前问过类似的问题 - ( 字母和数字排列的 SQL 匹配)。不同之处在于我发现我无法将正则表达式与我正在使用的 ADO.Net 驱动程序一起使用。而且,我无法更新它,因为我使用的是 Visual Studio 2003,它与更高版本不兼容。]
Is it possible to write an SQL statement for MySQL which finds all rows which match a specific arrangement of letters/numbers without using REGEXP?
i.e. SELECT myCol FROM myTable WHERE myCol='{number}{number}{letter}'
Should return 12X and 34Y but not 123X or 34YY
[I have asked a similar question before- (
SQL match on letter and number arrangement). The difference is that I have discovered that I cannot use regular expressions with the ADO.Net driver I am using. Whatsmore, I cannot update it since I am using Visual Studio 2003 which is not compatible with later versions.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个:
Try this:
嗯,是的,但是它会非常慢并且占用资源......
它将匹配“12X”和“34Y”,但不匹配“123X”或“34YY”。
Well, yes, but it would be very slow and resource-intensive ...
That'll match "12X" and "34Y" but not "123X" or "34YY".
只是为了明确说明接受的答案:(
肯的答案和安德烈的建议的混合)
Just to state the accepted answer explicitly:
(mixture of Ken's answer and Andriy's suggestion)
粗略的例子,但应该可以帮助你解决问题。由于您的示例不够精确,因此无法提供更多帮助。
Rough example, but should help you figure the problem out. Can't help more since your example isn't that precise.
如果您的查询与示例一样简单(即没有参数),那么您可以使用存储过程使用 Reg Exp 返回结果吗?在这种情况下,驱动程序不应该知道或关心您正在使用 Reg Exp。
If your query is as simple as your example (ie. no parameters) then can you use a Stored Procedure to return the results using Reg Exp? In that case the driver shouldn't know or care that you're using Reg Exp.
试试这个 -
http://dev.mysql.com/doc/refman/ 5.1/en/regexp.html
Try this -
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
如果您使用 EMS SQL Manager for MySQL,则可以使用 RegEx 来过滤数据。
正则表达式:
^
= 以[[:alpha:]]
开头 = 字母[[:digit:]]
= 数字{n }
= 恰好 n 个实例希望这会有所帮助。
If you are using EMS SQL Manager for MySQL, you can use RegEx to filter data.
RegEx:
^
= starts with[[:alpha:]]
= alphabet[[:digit:]]
= numbers{n}
= exactly n instances ofHope this helps.
您可以使用 LIKE 测试前两位数字,并使用 ascii 范围测试第三位数字。前两位数字也可以使用 ascii 范围,但 LIKE 应该更好。
You can test the first two digits using LIKE, and for the third use an ascii range. The first two digits can use an ascii range too, but LIKE should be better.