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
Related
I struggle through a complex query and need help with a REGEXP_SUBSTR command being used in the WHERE CLAUSE:
In deed this are two questions.
given three possible records, I want to get the part between the brackets, but only if they match something that looks like an ip. In one case the question is simply between brackets like "[192.168.178.21]"
other case is the text is like "sender=<some192-168-178-12.example.here>"
Case 1:
SELECT REGEXP_SUBSTR('This is a sample of [192.168.178.12] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender
SELECT REGEXP_SUBSTR('This is a sample of [dyn-192-168-178-12.example.com] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender
SELECT REGEXP_SUBSTR('This is a sample of [only.example com] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender
The problem above is that escaping does not work the way I've expected.
Expected would be:
127.0.0.1
dyn.127.0.0.1.example.com
NULL
Case 2:
If I search in the field using the WHERE CLAUSE I have limited success with
WHERE ( sndtext RLIKE ' sender=\\<.*[0-9]{1,3}(.|-|_)[0-9]{1,3}(.|-|_)[0-9]{1,3}(.|-|_)[0-9]{1,3}.*\\>')
but the match is too broad, it needs to stop at the '>'
How can I achieve both solution in a WHERE CLAUSE?
Might not be perfekt but my solution was (less escaping):
SELECT
REGEXP_SUBSTR(msgtext, '(?<=[[])[^]]+') AS ip,
REGEXP_SUBSTR(msgtext,'(?<=[ ]sndtext=<)[^>]+') AS txt
FROM txtmessage m
WHERE msgtext RLIKE ' sndtext=<.*([0-9]{1,3}[[:punct:]]){3}[0-9]{1,3}[^>]'
This works in the MySQL online tester if I'm understanding what you are looking for correctly.
with tbl(id, data) as (
select 1, 'This is a sample of [192.168.178.12] containing relevant data' from dual union all
select 2, 'This is a sample of [dyn-192-168-178-12.example.com] containing relevant data' from dual union all
select 3, 'This is a sample of [only.example com] containing relevant data' from dual union all
select 4, 'sender=<some192-168-178-12.example.here>' from dual
)
select id, data
from tbl
where ( data RLIKE '^.*(\\[|\\<).*[0-9]{1,3}(\\.|\\-)[0-9]{1,3}(\\.|\\-)[0-9]{1,3}(\\.|\\-)[0-9]{1,3}.*(\\]|\\>).*$');
Suppose I have a long query in sqlite3 command line as follows:
select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id
where col1 like "%some text%";
Now I would like to try this query for different wildcards like "%some text 2%" and "%some text 3%".
I only know of two options here:
rewriting the query multiple times in the command line (a pain in the butt)
committing the whole thing to a script and executing with .read test.sql
Is there another way? Does there exist some function-like concept in sqlite3 which applies here? Ideally I would like to define the "function" and call it from the command line.
SQLite does not support functions.
What you could do, to save you from retyping the main part of the query, is create a view:
create view MyView as
select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id
Then you can add the WHERE clause whenever you want to execute it:
SELECT * FROM MyView
WHERE col1 LIKE ?;
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'
I would like to rename the columns in the results of a SELECT expression. Of course, the following doesn't work:
SELECT * AS foobar_* FROM `foobar`
As I'm new to SQL, I think I'm just missing a concept, tool, or keyword that would lead to the answer. A hint in the right direction would be appreciated. Thanks!
UPDATE
I'm looking for a generic way to do this, and MySQL-specific techniques are absolutely fine.
In short, I'm writing a tool that "exports" the results of MySQL queries to Google Spreadsheets (via the Google Data API). Some queries are joins, so to make columns unique I wanted to prefix all column names with their respective table names.
You can alias the column names one by one, like so
SELECT col1 as `MyNameForCol1`, col2 as `MyNameForCol2`
FROM `foobar`
Edit You can access INFORMATION_SCHEMA.COLUMNS directly to mangle a new alias like so. However, how you fit this into a query is beyond my MySql skills :(
select CONCAT('Foobar_', COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Foobar'
you have to rename each column
SELECT col1 as MyCol1,
col2 as MyCol2,
.......
FROM `foobar`
select column1 as xyz,
column2 as pqr,
.....
from TableName;
I am trying to select a field based on it meeting one of 3 criteria... and I'm not sure how to do this. I think a RegExp is probably the best method buy I'm unfamiliar with writing them.
Say I have the integer 123, I would like to match the following cases:
123 (thats 123 only with no spaces or other numbers after it)
123-10/12/2007 00:00 (thats 123 with a hyphen and a date, or actually it could be anything after the hyphen)
123_1014859 (thats 123 with an underscore, or again anything after the underscore)
Is there a way to do this using MySQL?
A regex is plausible, but it's not the best performing option. The last comparison put MySQL's regex support as being par with wildcarding the left side of a LIKE statement -- works, but the slowest of every option available.
Based on your example, you could use:
SELECT t.*
FROM YOUR_TABLE t
WHERE t.column LIKE '123-%'
OR t.column LIKE '123_%'
Another alternative, because OR can be a performance issue too, would be to use a UNION:
SELECT a.*
FROM YOUR_TABLE a
WHERE a.column LIKE '123-%'
UNION ALL
SELECT b.*
FROM YOUR_TABLE b
WHERE b.column LIKE '123_%'
UNION ALL will return all results from both tables; UNION removes duplicates, and is slower than UNION ALL for that fact.
select * from foo where bar regexp '^123-|_'
(not tested)
I would avoid using regex inside a SQL statement. Someone can correct me if I am wrong, but MySQL has to use another engine to run the regex.
SELECT * FROM table
WHERE field like "123"
OR field LIKE "123-%"
OR field like "123_%";