简体   繁体   中英

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

I have a MySQL database with a column containing part numbers . 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.

Yet when I delete the 2 spaces and then type them again, the search returns a result.

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.

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

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.

This is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 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)

What actually happens is that when the engine finds an ascii code, it transforms it into a visible character. The CHAR(9) for example is often transformed into a 'big space' usually equal to 2 or 4 spaces. But phpmyadmin might just decide to not do it that way.

Other example is the line feed (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. 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).

This is important to always treat characters with their ascii codes.


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. The best is probably to use a regular expression or at least identify the fixed pattern of these strings.

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.

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