I have successfully exported my table in MySQL to csv with columns. I used this guy's answer at Include headers when using SELECT INTO OUTFILE?
SELECT 'ColName1', 'ColName2', 'ColName3'
UNION ALL
SELECT ColName1, ColName2, ColName3
FROM YourTable
INTO OUTFILE '/path/outfile'
However, I want to export a query formula as a new column to be added to the csv file. I tried adding an extra calculated column after the second SELECT statement. MySQL gave me an error saying "The used SELECT statements have a different number of columns".
Example formula: SELECT CAST((ColName1 * ColName2) AS DECIMAL(7,2)) AS ColNameX. I'm not sure where to input it in my export statement.
For the UNION to work you must have the same number of columns and they must be of the same type. As you creating the header row as text then all of your columns in the second query must also be text. Like so:
SELECT 'ColName1', 'ColName2', 'ColName3', 'New Column'
UNION ALL
SELECT
ColName1
,ColName2
,ColName3
,CAST(CAST((ColName1 * ColName2) AS DEC(5,2)) AS CHAR)
FROM YourTable
INTO OUTFILE '/path/outfile'
Related
I have a table with a name field that can have values like:
CHECK_5_20170909
CHECK_1_20170809
CHECK_11_20170809
CHECK_11_20170909
I would now like to query all fields that have a _1_ in the name, but ONLY them.
I tried this: SELECT * FROM tblName WHERE name LIKE '%_1_%';
but that shows me _11_ AND _1_ in my results.
When I try it with CHECKWHATEVER1WHATEVER20170909 and LIKE %WHATEVER1WHATEVER% it works, are there any special rules for _ in a MySQL Query?
Changing it to another delimiter in the MySQL DB would create a hell of work, is there any "workaround"?
You need to add a '\' before each underscore, otherwise its interpreted as a random "wildcard" character.
select * from
(
select 'CHECK_5_20170909' col
union
select 'CHECK_1_20170809'
union
select 'CHECK_11_20170809'
union
select 'CHECK_11_20170909'
) t
where col like '%\_1\_%'
try this using REGEXP
SELECT * FROM tblName WHERE name regexp '_1_';
it will return exact matches record from column for more reference read here
I have two select statements which are as mentioned below
SELECT 'MY OUTPUT'
SELECT * FROM MY TABLE
On execution MY OUTPUT is printed first and then there is a gap for next select.
I want to use something like UNION to combine two statements.
I'm using:
SELECT 'MY OUTPUT' UNION
SELECT * FROM MY TABLE
But, I am getting error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
SELECT 'MY OUTPUT' # this query return one column
UNION
SELECT * FROM MY TABLE # this query return more than one column
number of columns must be the same
SELECT 'MY OUTPUT' UNION # return one column
SELECT column1 FROM MY TABLE # return one column now it will work
but i think you want to do this no?
SELECT 'MY OUTPUT',column1,Column2,column3 FROM MY TABLE
So here's what's happening:
The error you're getting (All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.) is because there are more columns in one select than the other.
You could use the same blank columns in your first query as there are in [My Table] which would make your query look like:
SELECT 'MY OUTPUT' , '', '' ,'', '' --(no. of columns should match those in MY TABLE)
UNION
SELECT * FROM [MY TABLE]
I'm guessing you want an excel style cell merge which is not possible as the output of a select query unfortunately.
If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.
I want to write column headers into my csv files, and cannot figure out how, using select .. into syntax.
I've visited this page, as well as looking at some SO posts on the subject. I am wondering if MySQL's select .. into provides a feature to write the column headers or if there's another way to do that, while still writing a .csv file. A plain select at the command line does write the column headers.
Ugly, but solves your proble,
select * INTO OUTFILE from (
select 'col1', 'col2', 'col3'
UNION ALL
select col1, col2, col3 from table_name) as t
What am I doing wrong with this:
$sql = "SELECT * FROM content
WHERE threadName LIKE '%$filter%'
ORDER BY lastUpdated desc
UNION SELECT *
FROM content
WHERE threadName NOT LIKE '%$filter%'
ORDER BY lastUpdated desc";
The first statement before the UNION works well on its own, but this one above returns:
mysql_fetch_array() warning - supplied argument is not a valid MySQL result resource
Am I right in believing that UNION will not return duplicate entries, in which case the second SELECT statement doesn't need to have the NOT LIKE but will just return everything that wasn't listed in the first statement.
EDIT: This query should get you the rows matched by filter first, followed by those not matched:
SELECT *
FROM content
ORDER BY
CASE WHEN threadName LIKE '%$filter%' THEN 0 ELSE 1 END,
lastUpdated DESC
Note that you should never SELECT *, list the necessary columns instead.
While using UNION we must use column names instead of '*'.
Here, I m going to create .csv file at specific location on the system with emailid validation expression by using mysql query as bellow.
select 'col1','col2','col3','col4' from tableName
UNION
SELECT col1,col2,col3,col4 FROM tableName WHERE col4 NOT REGEXP '^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9.-]#[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$'
INTO OUTFILE '/home/sachin/mysqloutput/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ''''
LINES TERMINATED BY '\n';