MySQL load data infile concatenate columns - mysql

I have a table like this
mytable(`id` int, `text` varchar(255))
Also I have cvs-like file
1 hello word
2 this is a test
They are separated by space(or something else)
So can I use LOAD DATA INFILE to load the file into the table? How can I do this..?

I would probably go for 2 tables... One is the final, the other has 2 fields just as your CSV.
First solution, in file followed by
insert into table2 select concat(field1, field2) from table1
The other solution is to automate the solution 1 using a trigger. I'm not sure you can trigger something at the end, so trigger for each line added...

Related

Finding ID and inserting it into another table

I have a table with two columns. ID and WORD. I've used the following query to insert several files into this table
LOAD DATA LOCAL INFILE 'c:/xad' IGNORE INTO TABLE words LINES TERMINATED BY '\n' (#col1) set word=#col1;
Now I'd like to find specific values and insert them into another table. I know based on this question that I can do the following
insert into tab2 (id_customers, value)
values ((select id from tab1 where customers='john'), 'alfa');
But I'd like to do this based on the files. For example:
Loop through each line of file xad and pass it's value to a query like the following
insert into othertable (word_id)
values ((select id from firsttable where word='VALUE FROM CURRENT LINE OF FILE'));
I can write a Java app to do this line by line but I figured it'd be faster to make MySQL do the work if possible. Is there a way to make MySQL loop over each line, find the ID, and insert it into othertable?
Plan A: A TRIGGER could be used to conditionally copy the id to another table when encountered in whatever loading process is used (LOAD DATA / INSERT .. SELECT .. / etc).
Plan B: Simply load the table, then copy over the ids that you desire.
Notes:
The syntax for this
insert into tab2 (id_customers, value)
values ((select id from tab1 where customers='john'), 'alfa');
is more like
insert into tab2 (id_customers, value)
SELECT id, 'alpha'
FROM tab1
WHERE customers = 'john'

Convert tab-delimited raw data stored as text string into Hive table?

I have tab-delimited raw data that I have stored as a text string in a Hive table. I would like to be able to grab the first, third, ... , x fields, and store the results into another table with field names of my choosing. For example, given the following:
raw_table:
Field1 Field2 Field3
01001 1 00-00-32-0-700-000.000
new custID_pin Hive table:
custID PIN
01001 00-00-32-0-700-000.000
Here's what I have so far:
DROP TABLE IF EXISTS custID_pin;
CREATE TABLE custID_pin AS
SELECT
[psuedocode: column1, column3, where columns are defined by the tab delimiter]
FROM raw_table;
How do I write the select statement to achieve the desired results mentioned above, in the case of a tab-delimited rather than fixed-width file.
something like this?
DROP TABLE IF EXISTS custID_pin;
CREATE TABLE custID_pin row format delimited fields terminated by '\t' STORED AS TEXTFILE AS select column1, column3 from raw_table where columns1="value";

insert all column as 1 column for blob file

Is it possible to direct insert all columns to 1 column for blob file. Or do I need to outfile first the table so that I can upload it as blob. If ever possible here is my imaginary query, something like this:
insert into blob("file") values ('va1','va2','val3')
Any suggestions/comments thanks in advance.
In mysql, what you're thinking of is concat_ws. This allows you to combine the results from multiple columns.
INSERT INTO mytable(value1)
SELECT CONCAT_WS(',', value1, value2, value3)
FROM table
LIMIT 1;
However, in this case, you don't use the VALUES syntax, you simply run your query.
Here is a fiddle
http://sqlfiddle.com/#!9/57abb/1/0

merge two big csv files

I have two the type of csv files,the first file's content is the following:
1 13733776062
2 13535581615
3 13987993374
4 13866603331
The second file's content is the following:
13535581615|1
13733776062|0
13866603331|0
13987993374|1
The first file's format of each line is:id number,the second file's format of each line is:number flag. They have a relationship field:number.
Each file has 10 million lines.
Now I want to combine the two files by the number field into a new file which contains 3 fields of id,number,flag of each line.I am using Java to do this.
Can someone tell me the best method for this work that consumes lower time?
This is task more appropriate for SQLite, not for Java. You can do it as follows:
$ sqlite3 database.db
sqlite> CREATE TABLE table1 (id int, number int);
sqlite> .separator " "
sqlite> .import t1.csv table1
sqlite> CREATE TABLE table2 (number int, flag int);
sqlite> .separator "|"
sqlite> .import t2.csv table2
sqlite> CREATE TABLE mytable AS
SELECT t1.id, t1.number, t2.flag
FROM table1 t1, table2 t2
WHERE t1.number=t2.number;
sqlite> SELECT * FROM mytable;
1|13733776062|0
2|13535581615|1
3|13987993374|1
4|13866603331|0
I would expect that it should work for 10 million lines very fast.
And of course, you can use SQLite JDBC to create and access new database from Java.
To make access faster, it is good idea to create appropriate indexes.

Duplicate all data in the same table MYSQL

I'm looking for a way I can duplicate all the rows in my database, I tried exporting it and then importing but I get the duplicate key error.
The reason is purely for testing purposes, I just want a load of dummy data in there to test the system I have out.
Is there a direct statement for this? Or is there a way to export all data except ID (or change ID to MAX(ID) + 1 or AUTO INCREMENT)?
You can try this:
INSERT INTO your_table_name(parent_id,priority,text,shortname,weighting,g_or_a,
dept,ksf,day_start,day_end,date_start,date_end,depends_on,is_question,budget,
ccode,responsible,accountable,consulted,informed)
(SELECT parent_id,priority,text,shortname,weighting,g_or_a,dept,ksf,
day_start,day_end,date_start,date_end,depends_on,is_question,budget,ccode,
responsible,accountable,consulted,informed FROM your_table_name);
Firstly, insert one row in the table 'your_table_name'. Replace your_table_name with the actual table name in above code & execute the code repeatedly until it satisfies the required row numbers. I think it should work.
Put 1 record and then run:
insert into mytable select * from mytable
10 times. This will give you 1024 records. Continue until satisfied.
You could use an INSERT and the values would be a SELECT, just don't select the primary key and don't define it in the insert fields.
Imagine a table with 3 fields, the_pk, field_1, field_2
Something like
INSERT INTO the_table(field_1, field_2) (SELECT field_1, field_2 FROM the_table)