hi guys im new to mysql, i have trouble with displaying my table data, not sure what i am doing wrong..
this is my query for creating table:
CREATE TABLE sales20102017(
date DATE NOT NULL,
address VARCHAR(60) NOT NULL,
postal_code VARCHAR(10) NULL,
county VARCHAR (15) NOT NULL,
price BIGINT UNSIGNED NOT NULL,
not_full_market_price ENUM('Y', 'N') NOT NULL,
vat_exclusive ENUM('Y', 'N') NOT NULL,
property_description VARCHAR (30) NOT NULL,
size_description VARCHAR(40) NULL
);
and my load data:
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY '\n'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
and my excel data:
excel data
this is the result i get when i display it on mysql
mysql result
the table is not in order and everything is just messed up
Check for fields containing commas in csv file import on Mysql.
LOAD DATA INFILE "C:/xampp/mysql/bin/sales20102017.csv"
INTO TABLE sales20102017
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
See more on MySQL Reference Manual.
Related
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.
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:
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
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.
Running into Error Code: 1265 with the following code and data.
Data was exported as CSV.
The score column containing float data appears is source of issue as without this column the data loading works.
I have tried using Decimals and this works but I curious why floats do not work yet i'm working small figures.
CREATE TABLE dummy(
entry_id INT NOT NULL,
first_name VARCHAR(15) NOT NULL,
last_name VARCHAR(15) NOT NULL,
score FLOAT NOT NULL,
PRIMARY KEY (entry_id)
);
LOAD DATA INFILE "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/test_data.csv"
INTO TABLE dummy
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
Below is the sample csv file:
entry_id,first_name,last_name,score
1,Joshua,Mbweka,3.56
2,Mary,Njoroge,25.23
3,Adam,Kimanzi,64.41
4,Eve,Faluna,56.13
5,Lester,Chuma,89.21
under following sample im trying to import CSV data into MySQL table,
LOAD DATA INFILE 'file1.csv'
INTO TABLE loc_chg_log
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#col1,#col2,#col3,#col4,#col5) set failename=#col1,path=#col2,==blank=#col3,!=blank=#col4,++code=#col5;
MySQL table query is like this,
CREATE TABLE `log`.`loc_chg_log` (
`id` INT NOT NULL AUTO_INCREMENT,
`filename` VARCHAR(225) NULL,
`path` VARCHAR(225) NULL,
`==blank` VARCHAR(45) NULL,
`!=blank` VARCHAR(125) NULL,
`++code` VARCHAR(45) NULL
PRIMARY KEY (`id`));
none of these methods work's since the column name includes special character's, im getting error "unknown column'==blank' in field list"
1. '==blank'=#col5
2. ("`!=blank`=#col7")
3. [==blank=#col5]
by '==blank'=#col5 putting backticks in the column name, I was able to write on data in MySQL database. thank you
I'm trying to find a way to load a mysql table which I call ipCompare_tbl with IP addresses from a CSV file called myIPs.csv.
The fields in ipCompare_tbl are ipStart, and ipEnd. I've also included an auto_incrementor for a primary key generator field called id.
My goal is to have ipCompare_tbl loaded with:
ipStart ipEnd id
123.10.0.0 123.0.0.255 1
130.20.0.0 130.0.0.255 2
140.30.0.0 140.0.0.255 3
I keep getting the following error:
ERROR 1366 (HY000): Incorrect integer value: '"130.20.0.0"' for column 'ipStart' at row 1
I'm running the following code for it:
DROP TABLE IF EXISTS ipCompare_tbl;
CREATE TABLE ipCompare_tbl(
ipStart int(10) unsigned NOT NULL,
ipEnd int(10) unsigned NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY ( id ));
LOAD DATA INFILE 'myIPs.csv' INTO TABLE ipCompare_tbl
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ';'
IGNORE 1 LINES;
SELECT INET_ATON(ipStart), INET_ATON(ipEnd)
FROM ipCompare_tbl;
Change your table schema to
CREATE TABLE ipCompare_tbl(
ipStart varchar(15) NOT NULL,
ipEnd varchar(15) NOT NULL,
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY ( id ));
You need to convert the IP addresses to numbers when you're loading the CSV.
LOAD DATA INFILE 'myIPs.csv' INTO TABLE ipCompare_tbl
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY ';'
IGNORE 1 LINES
(#ipstart, #ipend, id)
SET ipStart = INET_ATON(#ipstart), ipEnd = INET_ATON(#ipend);
Then you don't need to use INET_ATON when you're selecting, since it's already numbers.
There is a CSV file with the following data:
1;8-25-2010;0:05;210;4
2;8-25-2010;2:45;412;5
3;8-25-2010;3:40;300;3
4;8-25-2010;4:45;226;6
5;8-25-2010;5:20;206;4
6;8-25-2010;5:25;216;3
And there is MySQL Table:
CREATE TABLE IF NOT EXISTS `Schedule` (
`ID` SMALLINT NOT NULL AUTO_INCREMENT,
`Num` INT(10),
`PlannedDate` DATE,
`PlannedTime` TIME NOT NULL,
`resQty` INT(3) NOT NULL,
`stID` VARCHAR(10) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY `stID` (`stID`) REFERENCES Stands (`stID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now I need to fill this table with the data from CSV file. For this I'm using the following code:
TRUNCATE TABLE testDB.Schedule;
LOAD DATA LOCAL INFILE 'C:\\temp\\Input.csv'
INTO TABLE testDB.Schedule FIELDS TERMINATED BY ';' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' (Num,PlannedDate,PlannedTime,stID,resQty);
But the error message says that "Data truncated for column PlannedDate at row1", ErrorNr. 1265. The same error message for all rows.
The date has the wrong format, it should be 2012-01-19
If you are stuck with that date format (which is not the MySQL default), you can load those dates into a temporary variable and then convert it to a date, like this:
TRUNCATE TABLE testDB.Schedule;
LOAD DATA LOCAL INFILE 'C:\\temp\\Input.csv'
INTO TABLE testDB.Schedule FIELDS TERMINATED BY ';' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(Num,#PlannedDate,PlannedTime,stID,resQty)
SET PlannedDate = STR_TO_DATE(#PlannedDate,'%m-%d-%Y');
in this line:
(Num,PlannedDate,PlannedTime,stID,resQty);
the two last parameters are reversed: stID,resQty
it SHOULD be:
resQty, stID
This is why you are getting a truncation error.