不使用正则表达式对字母和数字排列进行 SQL 匹配

发布于 2024-10-18 12:18:31 字数 425 浏览 1 评论 0原文

是否可以为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

对你的占有欲 2024-10-25 12:18:31

试试这个:

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) >= 'A' 
AND SUBSTRING(myCol, 1 , 1) <= 'Z' 
AND SUBSTRING(myCol, 2 , 1) >= 'A' 
AND SUBSTRING(myCol, 2 , 1) <= 'Z' 
AND SUBSTRING(myCol, 3 , 1) >= '0' 
AND SUBSTRING(myCol, 3 , 1) <= '9'

Try this:

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) >= 'A' 
AND SUBSTRING(myCol, 1 , 1) <= 'Z' 
AND SUBSTRING(myCol, 2 , 1) >= 'A' 
AND SUBSTRING(myCol, 2 , 1) <= 'Z' 
AND SUBSTRING(myCol, 3 , 1) >= '0' 
AND SUBSTRING(myCol, 3 , 1) <= '9'
再浓的妆也掩不了殇 2024-10-25 12:18:31

嗯,是的,但是它会非常慢并且占用资源......

SELECT 
    myCol 
FROM 
    myTable 
WHERE 
    CHAR_LENGTH(myCol) = 3 
    AND SUBSTRING(myCol, 1, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 2, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 3, 1) IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z')

它将匹配“12X”和“34Y”,但不匹配“123X”或“34YY”。

Well, yes, but it would be very slow and resource-intensive ...

SELECT 
    myCol 
FROM 
    myTable 
WHERE 
    CHAR_LENGTH(myCol) = 3 
    AND SUBSTRING(myCol, 1, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 2, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 3, 1) IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z')

That'll match "12X" and "34Y" but not "123X" or "34YY".

朱染 2024-10-25 12:18:31

只是为了明确说明接受的答案:(

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 2 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 3 , 1) BETWEEN '0' AND '9'

肯的答案和安德烈的建议的混合)

Just to state the accepted answer explicitly:

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 2 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 3 , 1) BETWEEN '0' AND '9'

(mixture of Ken's answer and Andriy's suggestion)

吃→可爱长大的 2024-10-25 12:18:31
SELECT myCol 
FROM   myTable 
WHERE  ( myCol LIKE '12X%' OR myCol LIKE '34Y%' ) 
AND    myCol NOT LIKE '34YY%'

粗略的例子,但应该可以帮助你解决问题。由于您的示例不够精确,因此无法提供更多帮助。

SELECT myCol 
FROM   myTable 
WHERE  ( myCol LIKE '12X%' OR myCol LIKE '34Y%' ) 
AND    myCol NOT LIKE '34YY%'

Rough example, but should help you figure the problem out. Can't help more since your example isn't that precise.

£烟消云散 2024-10-25 12:18:31

如果您的查询与示例一样简单(即没有参数),那么您可以使用存储过程使用 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.

风吹雨成花 2024-10-25 12:18:31

试试这个 -

SELECT myCol FROM myTable WHERE myCol REGEXP '[[:alpha:]]{2}[[:digit:]]{1}'

http://dev.mysql.com/doc/refman/ 5.1/en/regexp.html

Try this -

SELECT myCol FROM myTable WHERE myCol REGEXP '[[:alpha:]]{2}[[:digit:]]{1}'

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

殊姿 2024-10-25 12:18:31

如果您使用 EMS SQL Manager for MySQL,则可以使用 RegEx 来过滤数据。

SELECT col 
FROM tbl
WHERE col REGEXP '^[[:alpha:]]{2}[[:digit:]]{1}

正则表达式:

^[[:alpha:]]{2}[[:digit:]]{1}$

^ = 以

[[:alpha:]] 开头 = 字母

[[:digit:]] = 数字

{n } = 恰好 n 个实例

希望这会有所帮助。

正则表达式:

^ = 以

[[:alpha:]] 开头 = 字母

[[:digit:]] = 数字

{n } = 恰好 n 个实例

希望这会有所帮助。

If you are using EMS SQL Manager for MySQL, you can use RegEx to filter data.

SELECT col 
FROM tbl
WHERE col REGEXP '^[[:alpha:]]{2}[[:digit:]]{1}

RegEx:

^[[:alpha:]]{2}[[:digit:]]{1}$

^ = starts with

[[:alpha:]] = alphabet

[[:digit:]] = numbers

{n} = exactly n instances of

Hope this helps.

RegEx:

^ = starts with

[[:alpha:]] = alphabet

[[:digit:]] = numbers

{n} = exactly n instances of

Hope this helps.

ゞ花落谁相伴 2024-10-25 12:18:31

您可以使用 LIKE 测试前两位数字,并使用 ascii 范围测试第三位数字。前两位数字也可以使用 ascii 范围,但 LIKE 应该更好。

select *
from tbl
where length(n) = 3
  and '0123456789' like concat('%',mid(numcol,1,1),'%')
  and '0123456789' like concat('%',mid(numcol,2,1),'%')
  and ASCII(upper(MID(numcol,3,1))) between 65 and 90

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.

select *
from tbl
where length(n) = 3
  and '0123456789' like concat('%',mid(numcol,1,1),'%')
  and '0123456789' like concat('%',mid(numcol,2,1),'%')
  and ASCII(upper(MID(numcol,3,1))) between 65 and 90
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文