简体   繁体   English

MySQL搜索包含空格的值返回空结果(无行)

[英]MySQL search for value containing spaces returns empty result (no rows)

I have a MySQL database with a column containing part numbers . 我有一个MySQL 数据库 ,其中包含一个包含部件号的列。 Some of the part numbers contain spaces : 部分编号包含空格

3864205010  J

When I query the database or search for the part in phpMyAdmin no results are returned. 当我查询数据库或在phpMyAdmin 搜索该部分时,不会返回任何结果。

Yet when I delete the 2 spaces and then type them again, the search returns a result. 然而,当我delete 2个空格然后再次键入它们时,搜索会返回结果。

This query does not return a result: 此查询不返回结果:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010  K'

This query returns the result: 此查询返回结果:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010  K'

They look the same but in the second query I have deleted the 2 spaces before "K" and typed the spaces again. 它们看起来相同但在第二个查询中我删除了“K”之前的2个空格并再次输入空格。

If you can use wildcard instead of spaces: 如果您可以使用通配符而不是空格:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010%K'

yes as updated question if you wish to remove more space between which contents might be 3 or 4 space below query will use full to you 是更新的问题,如果你想删除更多的空间,其中的内容可能是3或4以下的空格,查询将使用full

SELECT REPLACE( REPLACE( part_no, " ", " " ), " ", " " ) from parts.

let me know if it is work for you ? 让我知道它是否适合您?

SELECT * 
FROM  `parts` 
WHERE  REPLACE(REPLACE(`part_no`, CHAR(9), ''),' ','') LIKE  REPLACE(REPLACE('3864205010  K', CHAR(9), ''),' ','')

This will probably work if part_no and/or search string has tabs and/or spaces. 如果part_no和/或搜索字符串包含制表符和/或空格,则这可能有效。

This is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 13). 这可能不是空格而是HTAB(ascii代码9)或甚至换行/回车(10和13)。 Copy paste in a good text editor, you'll see what it really is. 在一个好的文本编辑器中复制粘贴,你会看到它到底是什么。

Now, regarding to your wonder about why it doesn't work even if it does look like a space, this is because every single character we see is interpreted by the engine (notepad, phpmyadmin, firefox... any software with text rendering) 现在,关于你为什么它不工作的奇迹,即使它看起来像一个空格,这是因为我们看到的每一个字符都由引擎解释(记事本,phpmyadmin,firefox ......任何带文本渲染的软件)

What actually happens is that when the engine finds an ascii code, it transforms it into a visible character. 实际发生的是,当引擎找到ascii代码时,它会将其转换为可见字符。 The CHAR(9) for example is often transformed into a 'big space' usually equal to 2 or 4 spaces. 例如,CHAR(9)经常被转换成通常等于2或4个空格的“大空间”。 But phpmyadmin might just decide to not do it that way. 但phpmyadmin可能只是决定不这样做。

Other example is the line feed (CHAR(10)). 其他示例是换行(CHAR(10))。 In a text editor it would be the signal that the line ends, and (under unix systems mostly) a new line has to start. 在文本编辑器中,它将是该行结束的信号,并且(在大多数unix系统下)必须启动新行。 But you can copy this line feed into a database field, you're just not sure about how it is going to render. 但是您可以将此换行符复制到数据库字段中,您只是不确定它将如何呈现。 Because they want you to see everything in the cell they may choose to render it as a space... but that's NOT a space if you look at the ascii code of it (and here there's no trick, all rendering engines will tell you the right ascii code). 因为他们希望你看到单元格中的所有内容,他们可能会选择将其渲染为空格......但是如果你看一下它的ascii代码那就不是空格了(这里没有技巧,所有渲染引擎都会告诉你对ascii代码)。

This is important to always treat characters with their ascii codes. 始终使用ascii代码处理字符非常重要。


there's an answer above that suggests using a wildcard instead of the spaces. 上面的答案建议使用通配符而不是空格。 That might match, or just might not. 这可能匹配,或者可能不匹配。 Let's say your string is '386420K5010', so it is not the one you're looking for, still the LIKE '3864205010%K' pattern would return it. 假设你的字符串是'386420K5010',所以它不是你想要的那个,仍然是LIKE'3864205010%K'模式会返回它。 The best is probably to use a regular expression or at least identify the fixed pattern of these strings. 最好的可能是使用正则表达式或至少识别这些字符串的固定模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM