sql query result to csv file directly from console - sql

I want to export select query result with two columns to csv file. I don't have phpmyadmin or tools like this. I have to run my query directly from console. Can someone give me a hint how can I do this?

You could try to use this command:
SELECT field_1,field_2
FROM table_name
INTO OUTFILE '/tmp/output.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Related

Hive- query output to file csv/excel

I am trying to output the results of Hive to a File (preferably excel) I tried below methods and non of them work as explained in most posts. I wonder because I use Hue environment. I am new to Hue and hive, any help would be appreciated
insert overwrite directory 'C:/Users/Microsoft/Windows/Data Assets' row format delimited fields terminated by '\n' stored as textfile select * from final_table limit 100;
INSERT OVERWRITE LOCAL DIRECTORY 'C:/Users/Microsoft/Windows/Data Assets'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\n'
STORED AS TEXTFILE
select * from final_table limit 100;
I have tried running the same query in my setup, it works fine.
In your case , it might be an issue with 'C:/Users/Microsoft/Windows/Data Assets' folder permission.
Try writing to different folder(User's Home folder).
Query:
INSERT OVERWRITE LOCAL DIRECTORY '/tmp/fsimage' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\n' STORED AS TEXTFILE SELECT * FROM stream_data_partitioned limit 100;
Output Attached
Destination folder

How to transfer Excel data to My SQL Query Browser?

I have a problem to transfer Excel data to My SQL Query Browser? Someone can help me with how to transfer Excel data to SQL?
My excel data is store
C:\2019\countries.xlsx
My excel data is like below the picture:
Below is my SQL info, I won't put excel file data to column(id, name,country_code and language):
I hope anyone can guide me on how to use an easy way to insert data into the database. Thanks.
You need to convert into csv then use the below query to import.
load data local infile 'C:/2019/countries.csv' into table countries
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 lines;
You can refer the detail in docs here
Note: If using Windows terminate by '\r\n' use '\n' on Linux based OS.

Derby SQL commands to export to CSV file

I am very new to Apache Derby, and I have a need to export all of the rows from a table into a csv file.
I am using RazorSQL to connect to the database.
What I need to know is if there is an equivalent Derby SQL command to what MySQL does to export data below.
NOTE: VERY IMPORTANT. One of the columns in this database table is defined as an XML datatype. It will NOT work using any kind of wizard to export the data, because the select statement needs to use the 'xmlserialize' statement in the SQL command.
Here is what I want to do, but Derby does not understand the "INTO OUTFILE" statement I am used to using with MySQL.
select
message_id,
msg_schema_id,
user_id,
req_send_time,
destination,
xmlserialize(msg_content as clob) as message,
facility_id
from Audit
into outfile 'd:\csv\audit.csv'
fields terminated by ','
enclosed by '"'
lines terminated by '\n';

Importing CSV to MySQL with Load Data Infile

I'm trying to load a CSV file into MySQL, and I keep getting syntax errors:
load data infile 'c:/worldminwage.csv' fields terminated by ',' enclosed
by '"' lines terminated by '\n' (YearlyWage, PercentGDP, Date
effective);
Can anyone help me get this working? Thanks.
Valid syntax is LOAD DATA INFILE 'c:/worldminwage.csv' INTO TABLE tablename.
You forgot to mention which table the data should go in. See LOAD DATA INFILE Syntax
I think This would be more batter
LOAD DATA INFILE 'c:/worldminwage.csv' fields terminated by ',';
Because you will get rid of error like columns numbers are not matching in the .csv file and mysql table

MySQL creating and populating the database

I have downloaded the MySQL 5.2.5.1 version. I need to create the database and populate it. O have the data file in .csv format. I'm running the Mysql on windows. So when I create the database and a table in it, I want to populate the data from a file on my PC which is in .csv format.
I did it in this way:
load data local infile 'uniq.csv' into table tblUniq
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
But it is not working for me. Where I am making a mistake?
You might need to change
lines terminated by '\n'
to
lines terminated by '\r\n'