Home > Software engineering >  Mysql: Varchar(3) can't find input when I select something that is 2 characters long
Mysql: Varchar(3) can't find input when I select something that is 2 characters long

Time:02-20

I have this table:

CREATE TABLE `country` (
  `name` VARCHAR(60) NOT NULL,
  `code` VARCHAR(3) UNIQUE NOT NULL,
  PRIMARY KEY  (`code`)
);

As you can see the primary key of this table is the word code When I try to select a specific code in this table, that is 2 characters long, it cannot find anything.

On the other hand, when I select a 3 characters long code like this: select * from `country` where `code` = "TZA";

I get the result I want

I searched for my variable in the table (for example the code "AL") and it appears to be registered.

enter image description here

Why is this happening and how could I make it work?

Thank you in advance!

I am importing my data from a csv file that looks like this:

enter image description here

LOAD DATA LOCAL INFILE 'path_to_file\\countries.csv'
INTO TABLE `country`
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(`name`, `code`);

I have tried selecting with a space in the end of the code and on the front of it:

select * from `country` where `code` = 'AL ';
select * from `country` where `code` = ' AL';

But they output nothing

CodePudding user response:

The real solution is:

When importing this CSV file you should use:

LOAD DATA LOCAL INFILE 'path_to_file\\countries.csv'
INTO TABLE `country`
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(`name`, `code`);

Because your lines seems to be terminated the way Windows terminates lines.

  • Related