3

I have a table Types that has the following columns: ID, Name, Type. The table is filled with about 300 rows. One of the rows:

ID    Name    Type
------------------
1     BMW     S 1000 RR

The following query returns this row:

SELECT * FROM Types WHERE Name = 'BMW'

However, the following query returns nothing:

SELECT * FROM Types WHERE Type = 'S 1000 RR'

There are no extra spaces in the Type, and the data types of Name and Type are exactly the same (varchar 255, utf8_unicode_ci). What can possibly cause this?

I am using MySQL, InnoDB. Using phpMyAdmin I get the exact same results, so no typo's in column names...

9
  • I find it very surprising that the query is working at all in either case. types and type are both MySQL reserved words and shouldn't really have worked without backtick escaping. Commented Apr 7, 2016 at 8:15
  • 3
    @apokryfos that is not correct. Niether of them is Reserved and niether of them requires backticks Commented Apr 7, 2016 at 8:18
  • @HankyPanky this page seems to claim otherwise Commented Apr 7, 2016 at 8:19
  • @apokryfos No, it shows they are key words, and not reserved. Commented Apr 7, 2016 at 8:19
  • @apokryfos do you see an (R) infront of either of those terms? sqlfiddle.com/#!9/e48d6/1 Commented Apr 7, 2016 at 8:19

3 Answers 3

3

I've found the problem: to fill the table I am reading a textfile per line. The newline character was the problem, it is invisible in phpMyAdmin's browse table view, but I saw it when editing a single row.

The following query fixed my problem:

UPDATE Types SET Type = REPLACE(REPLACE(Type, '\r', ''), '\n', '');

Found in How to remove new line characters from data rows in mysql?

Thanks everyone for your comments.

Sign up to request clarification or add additional context in comments.

3 Comments

I think that's better if you mark it as solved, so the next guy that comes here with the same problem can also be helped :-)
Can't do that before 2 days are over, wanted to do it right away... Will do it asap.
Very Thanks, It works like charm. I have been struggling for last 2 weeks this issue. Thank you very much. +1
0

I am sure that's due to trim

Try this

 SELECT * FROM Types WHERE  TRIM(Type) = 'S 1000 RR'

1 Comment

Unfortunately, no results
0

Its not like that. Your table name and column names are all good and the query is also giving correct o/p: go CREATE TABLE Types ( ID int, Name varchar(3), Type varchar(9));

go
INSERT INTO Types (ID, Name, Type) VALUES (1, 'BMW', 'S 1000 RR')

SELECT * FROM Types WHERE Type = 'S 1000 RR'

2 Comments

It was a newline character at the end that screwed it up. See my answer for details.
okok..i was tryin in ssms actually

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.