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?
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
Long story short, I need to be able to combine two tables (one is a continuation of the previous with a little overlap). I then need to set one column to be a Primary Key, which means no duplicates. When I tried to just use the Import Wizard in SQL Management Studio and import one table into the other, it just added all the data from one table into the next. I need to figure out some way or SQL command that will import all the data from the new table into the older table, and replace any existing duplicate data with the ones from the newer table.
Think of it like this, I have two tables with the following data:
People_Old table
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Green |
| John | Red |
+------+--------+
People table
+------+--------+
| Name | Color |
+------+--------+
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
Assuming the "Name" column are suppose to be a Primary Key, I would like to add the data from the newer "People" table into the older "People_Old" table, but replace the overlapping data so there are no duplicates. In this example, I would like the final "People_Old" table to be:
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
Use the REPLACE statement, something like:
replace into people_old select * from people;
DROP TABLE IF EXISTS old;
CREATE TABLE old
(Name VARCHAR(12) NOT NULL PRIMARY KEY
,Color VARCHAR(12) NOT NULL
);
INSERT INTO old VALUES
('Mary','Blue'),
('Katy','Yellow'),
('Jim','Green'),
('John','Red');
DROP TABLE IF EXISTS new;
CREATE TABLE new
(Name VARCHAR(12) NOT NULL PRIMARY KEY
,Color VARCHAR(12) NOT NULL
);
INSERT INTO new VALUES
('Jim' ,'Silver'),
('John','Brown'),
('Greg','Purple'),
('Liz' ,'Pink');
SELECT * FROM old;
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Green |
| John | Red |
+------+--------+
SELECT * FROM new;
+------+--------+
| Name | Color |
+------+--------+
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
INSERT INTO old
SELECT name
, color
FROM new
ON DUPLICATE KEY UPDATE old.color = new.color;
SELECT * FROM old;
+------+--------+
| Name | Color |
+------+--------+
| Mary | Blue |
| Katy | Yellow |
| Jim | Silver |
| John | Brown |
| Greg | Purple |
| Liz | Pink |
+------+--------+
6 rows in set (0.00 sec)
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.
In my app, I allow people to create additional rooms for inventory purposes. Let's say I have a table called "user_data." The d_key field is the data type for a lack of a better description. I have that just in case I introduce more custom fields for the user. Anyways, I want every user to have a room that's called None. This way any item that doesn't have a room assigned to it will default to "None."
What's the best way to introduce None into the table? Users won't be able to delete this default room, but they can edit/delete rooms they input into the DB table.
user_data table
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| d_key | varchar(20) | NO | | NULL | |
| d_value | varchar(40) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Sample data:
+----+---------+----------+--------------+
| id | user_id | d_key | d_value |
+----+---------+----------+--------------+
| 20 | 2 | location | bedroom |
| 21 | 2 | location | living room |
| 22 | 2 | location | attic |
| 23 | 3 | location | kitchen |
+----+---------+----------+--------------+
Add the row as part of your user creation process.
For existing users, loop over the users and ensure they have a "none" room; if they don't, insert one.