I imported a csv into a table in a MySQL database, but when I do SELECT * FROM hale (where hale is the name of the table), I get something that looks like this:
+----+--------+------+---------+-----------------+-----------------+----------------------------------------+
| id | Source | Page | Country | Word_ID | OriginalWord | OriginalTranslation |
+----+--------+------+---------+-----------------+-----------------+----------------------------------------+
| | word:HH1885:001 | Siksika | Blackfeet
|rd:HH1885:003 | oqkatsh | foot
|d:HH1885:004 | nitokiskam | one
|d:HH1885:005 | natokam | two
|ord:HH1885:006 | newowiskam | three
|rd:HH1885:007 | nijoim | four
|rd:HH1885:008 | nijitji | five
|d:HH1885:009 | nawo | six
|ord:HH1885:010 | ikitchike | seven
|ord:HH1885:011 | nanisho | eight
|rd:HH1885:012 | pikkiso | nine
|d:HH1885:013 | kepo | ten
|word:HH1885:014 | najippo | twenty
|word:HH1885:015 | neppo | thirty
|a | word:HH1885:016 | kepippo | one hundred
|d:HH1885:017 | omakkatose | God
|word:HH1885:018 | spouteh | heaven
|d:HH1885:019 | kristikoy | day
|ord:HH1885:020 | kokoy | night
|d:HH1885:021 | matapi | man
|ord:HH1885:022 | akew | woman
|d:HH1885:023 | saqkomapi | boy
The csv file is formatted like this:
id,Source,Page,Country,Word_ID,OriginalWord,OriginalTranslation
0,HH1885,702,Canada,word:HH1885:001,Siksika,Blackfeet
1,HH1885,702,Canada,word:HH1885:002,siksinam,black
2,HH1885,702,Canada,word:HH1885:003,oqkatsh,foot
3,HH1885,702,Canada,word:HH1885:004,nitokiskam,one
4,HH1885,702,Canada,word:HH1885:005,natokam,two
5,HH1885,702,Canada,word:HH1885:006,newowiskam,three
6,HH1885,702,Canada,word:HH1885:007,nijoim,four
7,HH1885,702,Canada,word:HH1885:008,nijitji,five
8,HH1885,702,Canada,word:HH1885:009,nawo,six
9,HH1885,702,Canada,word:HH1885:010,ikitchike,seven
10,HH1885,702,Canada,word:HH1885:011,nanisho,eight
11,HH1885,702,Canada,word:HH1885:012,pikkiso,nine
12,HH1885,702,Canada,word:HH1885:013,kepo,ten
13,HH1885,702,Canada,word:HH1885:014,najippo,twenty
14,HH1885,702,Canada,word:HH1885:015,neppo,thirty
15,HH1885,702,Canada,word:HH1885:016,kepippo,one hundred
And the commands I ran were:
CREATE TABLE hale (
id int NOT NULL AUTO_INCREMENT,
Source nvarchar(255) NOT NULL,
Page int NOT NULL,
Country nvarchar(255),
Word_ID varchar(255) NOT NULL,
OriginalWord nvarchar(255),
OriginalTranslation nvarchar(255),
PRIMARY KEY (id)
);
LOAD DATA LOCAL INFILE '/home/pearlh6527/sel_cols.csv'
INTO TABLE hale
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(id, Source, Page, Country, Word_ID, OriginalWord, OriginalTranslation);
When I select individual columns, however, I see that the information is present in the table. For example, SELECT Source FROM hale gives something like:
+--------+
| Source |
+--------+
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
| HH1885 |
Does anyone know why the information might not be displaying correctly when I select everything?
Related
I'm having two tables say(for example), Department and Members
Department table description:
CREATE TABLE `Department` (
`code` int(10) DEFAULT NULL,
`name` char(100) DEFAULT NULL,
KEY `code_index` (`code`),
KEY `name_index` (`name`)
)
Department table values:
+------+-------------+
| code | name |
+------+-------------+
| 1 | Production |
| 2 | Development |
| 3 | Management |
+------+-------------+
Members table description:
CREATE TABLE `Members` (
`department_code` int(10) DEFAULT NULL,
`name` char(100) DEFAULT NULL,
KEY `department_code_index` (`department_code`),
KEY `name_index` (`name`)
)
Members table values:
+-----------------+----------------+
| department_code | name |
+-----------------+----------------+
| 1 | Ross Geller |
| 1 | Monica Geller |
| 1 | Phoebe Buffay |
| 1 | Rachel Green |
| 1 | Chandler Bing |
| 1 | Joey Tribianni |
| 2 | Janice |
| 2 | Gunther |
| 2 | Cathy |
| 2 | Emily |
| 2 | Fun Bobby |
| 2 | Heckles |
| 3 | Paolo |
| 3 | Mike Hannigan |
| 3 | Carol |
| 3 | Susan |
| 3 | Richard |
| 3 | Tag |
+-----------------+----------------+
I want to get the all the department code and name for the given set of users. As i just want the department names alone, I used the below query.
mysql> select Department.code, Department.name, Members.department_code from Department left join Members on (Department.code=Members.department_code) where Members.name in ('Rachel Green', 'Gunther', 'Paolo') group by Department.code;
+------+-------------+-----------------+
| code | name | department_code |
+------+-------------+-----------------+
| 1 | Production | 1 |
| 2 | Development | 2 |
| 3 | Management | 3 |
+------+-------------+-----------------+
This works fine and the "explain" gives me below execution plan.
+----+-------------+------------+------------+------+----------------------------------+-----------------------+---------+----------------------+------+----------+---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+----------------------------------+-----------------------+---------+----------------------+------+----------+---------------------------------+
| 1 | SIMPLE | Department | NULL | ALL | code_index | NULL | NULL | NULL | 3 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | Members | NULL | ref | department_code_index,name_index | department_code_index | 5 | test.Department.code | 1 | 16.67 | Using where |
+----+-------------+------------+------------+------+----------------------------------+-----------------------+---------+----------------------+------+----------+---------------------------------+
But the "group by" uses temporary table which may degrade the performance if the Members table contains a lot of rows. Though I guess some ideal indexing would help out here, i can't get the proper idea. Any help will be appreciated.
Thanks in advance!
You can avoid the group by over all the data using a subquery:
select d.code, d.name, d.department_code
from Department d
where exists (select 1
from Members m
where d.code = m.department_code and
m.name in ('Rachel Green', 'Gunther', 'Paolo')
);
With an index on members(department_code, name), this should be much faster.
I have two similar data sets (table, view, CTE), one of which contains unique rows (guaranteed by DISTINCT or GROUP BY), the second contains duplicates (no primary key constraint involved).
How can I get the difference of two data sets so that I only get the duplicates of the second set in MySql 8?
Say I have a table called Animals, which stores NAME and SPECIES.
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| NAME | varchar(255) | YES | | NULL | |
| SPECIES | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
ANIMALS
+----+---------+-------------+
| ID | NAME | SPECIES |
+----+---------+-------------+
| 1 | Lion | Carnivorous |
| 2 | Giraffe | Herbivores |
| 3 | Zebra | Herbivores |
| 4 | Trutle | Herbivores |
| 5 | Tiger | Carnivorous |
| 6 | Bear | Carnivorous |
+----+---------+-------------+
With that in place, I define the view DUPLICATED.
CREATE VIEW DUPLICATED AS
SELECT * FROM ANIMALS
UNION ALL
SELECT * FROM ANIMALS WHERE SPECIES = "Carnivorous";
(Duplicates every Carnivorous in the set)
DUPLICATED
+---------+-------------+-----+
| NAME | SPECIES | CNT |
+---------+-------------+-----+
| Lion | Carnivorous | 2 |
| Tiger | Carnivorous | 2 |
| Bear | Carnivorous | 2 |
| Giraffe | Herbivores | 1 |
| Zebra | Herbivores | 1 |
| Trutle | Herbivores | 1 |
+---------+-------------+-----+
Now I want to get the difference of SELECT * FROM ANIMALS and DUPLICATED or vice versa, essential getting all Carnivorous from ANIMALS.
Basically you can group by whatever combination of fields that guarantee the uniqueness of a record in your result. you haven't provided your queries or your table's schema, so i will try to demonstrate this using a general example. you can get my drift and apply it to your query.
SELECT field1, field2, field3 COUNT(*)
FROM MyTable
GROUP BY field1, field2, field3
HAVING COUNT(*) > 1
I have a big csv (near 100mb) that I would like to import in a table with the following structure:
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| cep | varchar(255) | YES | MUL | NULL | |
| site | text | YES | | NULL | |
| cidade | text | YES | | NULL | |
| uf | text | YES | | NULL | |
| cepbase | text | YES | | NULL | |
| segmentacao | text | YES | | NULL | |
| area | text | YES | | NULL | |
| cepstatus | int(1) | YES | | NULL | |
| score | int(11) | NO | | NULL | |
| fila | int(11) | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
I was about to write some code to import but I've found a MySQL command that does the job to me. So I've write the following:
LOAD DATA LOCAL INFILE '/Users/user/Downloads/base.csv'
INTO TABLE cep_status_new
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#id,#cep,#site,#cidade,#uf,#cepbase,#segmentacao,#area,#cepstatus,#score,#fila)
SET id=NULL, cep=#col1, site='GOD', cidade=#col6, uf=#col7, cepbase='-', segmentacao=#col9, cepstatus=#col2, area='BING', score=99999, fila=5;
To try this code, I've removed thousand lines from my CSV and let only 2 lines: header and an input example:
cep,status,gang,bang,random,mock,awesome,qwert,hero
01019000,0,00387,00388,3550308,SAO PAULO,SP,011,B2
The code runs without problem but my insert is pretty strange:
mysql> select * from cep_status_new;
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
| id | cep | site | cidade | uf | cepbase | segmentacao | area | cepstatus | score | fila |
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
| 1 | 1 | GOD | 24655 | 3554805 | - | SP | BING | 0 | 99999 | 5 |
+----+------+------+--------+---------+---------+-------------+------+-----------+-------+------+
1 row in set (0.01 sec)
Why values from CSV are not being filled correctly?
According to this specification the column list after IGNORE 1 ROWS decides how the columns of the CSV file are mapped to columns of the table. It can either list the table columns in the order of the file or it can load the file columns into variables. With the column list
(#id,#cep,#site,#cidade,#uf,#cepbase,#segmentacao,#area,#cepstatus,#score,#fila)
you are loading 11 columns of the CSV file into variables named "id", "cep", etc. In the SET statement you then need to declare how the columns of the table are constructed from the variables. With the given statement you are refering to variables #col1 etc. that are not defined anywhere and consequently have undefined values.
The corrected statement (that I sadly can't test myself right now) should be:
INTO TABLE cep_status_new
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#col1,#col2,#col3,#col4,#col5,#col6,#col7,#col8,#col9)
SET id=NULL, cep=#col1, site='GOD', cidade=#col6, uf=#col7, cepbase='-', segmentacao=#col9, cepstatus=#col2, area='BING', score=99999, fila=5;
I have a table event as:
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| date | date | YES | | NULL | |
| type | varchar(15) | YES | | NULL | |
| remark | varchar(255) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
Now I wish to insert some data into this table from a text file event.txt. The text file is as:
Tommy 2000-01-02 litter 4 kittens, 3 females, 1 male
Bowser 2001-04-26 vet needed break straightened
puffball 2002-05-13 birthday gave him a new chew toy
The whitespaces after values are tabs. The last values are sentences with actual whitespaces. When I load this txt file into the table eventit doesn't load the values properly. I run the query:
load data local infile 'D:/Softwear/mysql/install/data/event.txt' into table event;
The table generated is as following:
+----------+------------+----------+-------------------------------+
| name | date | type | remark |
+----------+------------+----------+-------------------------------+
|Tommy | 2000-01-02 | litter | 4 kittens, 3 females, 1 male
|ser | 2001-04-26 | vet | needed break straightened
| puffball | 2002-05-13 | birthday | gave him a new chew toy |
+----------+------------+----------+-------------------------------+
The value bowser is truncated. Why is it so? When I uploaded a similar pet.txt file into table pet then the table generated correctly. I used notepad to write the txt file, used tab after values and newline after rows.
I have a question which relates to MySQL. The problem can be seen in these two images:
http://imgur.com/NrOwSxS,yPo9Cra
http://imgur.com/NrOwSxS,yPo9Cra#1
Does anyone know why MySQL is doing this? It should show up as a nice and neat table, not this bundle of gibberish. Thanks in advance! :D
First, to show that there's nothing really wrong, try this query:
SELECT firstname FROM contact_info
That should look good. Now try this:
SELECT firstname, lastname FROM contact_info
That's how you pick individual columns.
Really you want to capture output to a file, this page shows you how: The MySQL Command-Line Tool
Then you can learn to use other programs to format it nicely.
I assume you created your table somewhat like this:
create table automobile (make char(10),model char(10),year int, color char(10), style char(50), MSRP int);
insert into automobile values ('Ford','Mustang',2006,'Blue','Convertible',27000);
insert into automobile values ('Toyota','Prius',2005,'Silver','Hybrid',22000);
insert into automobile values ('Toyota','Camry',2006,'Blue','Sedan',26000);
insert into automobile values ('Dodge','1500',2005,'Green','Pickup',26000);
so a
describe automobile
will show you your columns as:
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| make | char(10) | YES | | NULL | |
| model | char(10) | YES | | NULL | |
| year | int(11) | YES | | NULL | |
| color | char(10) | YES | | NULL | |
| style | char(50) | YES | | NULL | |
| MSRP | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
as long as your columns in total are smaller than your terminal's width you should see
the expected result:
mysql> select * from automobile;
+--------+---------+------+--------+-------------+-------+
| make | model | year | color | style | MSRP |
+--------+---------+------+--------+-------------+-------+
| Ford | Mustang | 2006 | Blue | Convertible | 27000 |
| Toyota | Prius | 2005 | Silver | Hybrid | 22000 |
| Toyota | Camry | 2006 | Blue | Sedan | 26000 |
| Dodge | 1500 | 2005 | Green | Pickup | 28000 |
+--------+---------+------+--------+-------------+-------+
if you'd like the result smaller then pick the columns you'd like to see e.g.
select make,model from automobile
mysql> select make,model from automobile;
+--------+---------+
| make | model |
+--------+---------+
| Ford | Mustang |
| Toyota | Prius |
| Toyota | Camry |
| Dodge | 1500 |
+--------+---------+
to make the content of a column smaller you may use the left string function
select left(make,4) as make, left(model,5) as model,left(style,5) as style from automobile;
+------+-------+-------+
| make | model | style |
+------+-------+-------+
| Ford | Musta | Conve |
| Toyo | Prius | Hybri |
| Toyo | Camry | Sedan |
| Dodg | 1500 | Picku |
+------+-------+-------+
You can try supplying a line separator character in the end.
mysql> LOAD DATA LOCAL INFILE *file_path* INTO *table_name* LINES TERMINATED BY '\r\n';
Separator character may vary for editors. In Windows, most editors use '\r\n'.