I have a mySQL table which contains html code and some other information.
One example like this:
SELECT id, lang, html
FROM mytable t
WHERE type = 10
give the result:
id lang html
-------------------------------------------------------
20 fr '<html> ... html code here </html>'
21 de '<html> ... html code here </html>'
22 it '<html> ... html code here </html>'
23 en '<html> ... html code here </html>'
and my idea is to export the html code into one file per lines like
20_fr.html will contains the html columns for id 20
21_de.html will contains the html columns for id 21
etc...
Is it possible to do so in SQL language only like this example for exporting all rows into one file.
I know I can do it with Java or any other language but I am interested only by a SQL (a stored procedure could be ok).
You can use the SELECT statement with the INTO and OUTFILE clauses.
See: http://dev.mysql.com/doc/refman/5.7/en/select-into.html
SELECT html
FROM mytable
WHERE lang = 'fr'
INTO OUTFILE 'frdata.txt'
The following SQL query might be used to generate one file output statement per row in the table. Note the use of the CONCAT function to build a new SQL SELECT INTO OUTFILE command per row.
SELECT CONCAT( 'SELECT html from mytable where lang=''', lang, '''', ' INTO OUTFILE ''', CAST(id AS CHAR), '_', lang, '.html'';')
FROM mytable
ORDER BY id;
This will yield the the statements:
SELECT html from mytable where lang='fr' INTO OUTFILE '20_fr.html';
SELECT html from mytable where lang='de' INTO OUTFILE '21_de.html';
SELECT html from mytable where lang='it' INTO OUTFILE '22_it.html';
...
Related
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 am creating an external table as shown below
CREATE EXTERNAL TABLE '~\test.csv'
USING ( DELIMITER ',' Y2BASE 2000 ENCODING 'internal' REMOTESOURCE 'ODBC' ESCAPECHAR '\' )
AS SELECT * FROM TEST_TABLE;
It works fine. My question is :
Is there a way we can name the header values as column names in the test.csv file ? Is it possible in either Netezza or postgres.
I think we can do it using COPY, however I want to do it using the EXTERNAL TABLE command.
Thanks
In version 7.2 of Netezza you can now specify the IncludeHeader option to achieve this with external tables.
This is documented here
It's not pretty, and it would likely add some amount of overhead to the query, but you could do something like this:
CREATE EXTERNAL TABLE ... AS
SELECT ColumnId, OtherColumn
FROM (
SELECT FALSE as IsHeader, ColumnId::VARCHAR(512), OtherColumn::VARCHAR(512)
FROM TEST_TABLE
UNION ALL
SELECT TRUE as IsHeader, 'ColumnId', 'OtherColumn'
) x
ORDER BY IsHeader DESC
This is another example, along the same idea that qSlug gave...
CREATE EXTERNAL TABLE
'C:\HEADER_TEST.csv' USING
(DELIMITER '|' ESCAPECHAR '\' Y2BASE 2000 REMOTESOURCE 'ODBC') AS
--actual query goes here. Leave the 'data' field on there.
(select store_name, address1, 'data'
from yourtable
limit 10)
union
--field names go here. Leave the 'header' field on there.
select 'store_name', 'address1', 'header'
from _v_dual
order by 3 desc
You can then just delete the last column from your csv file.
There actually is a way to include the header in the file if you have Netezza version 7.2 or greater.
The option is 'includeheader' , but it doesn't look like the Aginity Workbench highlights 'includeheader' as though it's an option (at least in my version: 4.8).
CREATE EXTERNAL TABLE '~\test.csv'
USING
(
DELIMITER ','
Y2BASE 2000
ENCODING 'internal'
REMOTESOURCE 'ODBC'
ESCAPECHAR '\'
/****THIS IS THE OPTION ****/
INCLUDEHEADER
)
AS
SELECT *
FROM TEST_TABLE;
You'll notice that Aginity doesn't apply highlighting to the option but it will execute and write a header to the first row.
Let's say we have following table.
UserId | Message
-------|-------------
1 | Hi, have a nice day
2 | Hi, I had a nice day
I need to have all { Hi,-have-a-nice-day-I-had } words separately.
Is there any way to do that ? What if I want to export words from whole database tables ?
Similar results would be also good.
try this:In Sql server 2005 or above
create table yourtable(RowID int, Layout varchar(200))
INSERT yourtable VALUES (1,'hello,world,welcome,to,tsql')
INSERT yourtable VALUES (2,'welcome,to,stackoverflow')
;WITH SplitSting AS
(
SELECT
RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part
,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder
FROM YourTable
WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0
UNION ALL
SELECT
RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
UNION ALL
SELECT
RowID,Remainder,null
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)
SELECT part FROM SplitSting ORDER BY RowID
SQLFIDDLE DEMO
Well, ok, here it goes.
In SQL Server you can use this...
SELECT word = d.value('.', 'nvarchar(max)')
FROM
(SELECT xmlWords = CAST(
'<a><i>' + replace([Message], ' ', '</i><i>') + '</i></a>' AS xml)
FROM MyMessageTbl) T(c)
CROSS APPLY c.nodes('/a/i') U(d)
And I hope that for MySQL you can use the same thing, using XML support - ExtractValue() etc.
EDIT: explanation
- replace([Message], ' ', '</i><i>') replaces e.g. 'my word' with 'my</i><i>word'
- then I add the beginning and the end of xml -> '<a><i>my</i><i>word</i></a>', so I have a valid xml... and cast it to xml type to be able to do something with it
- I select from that xml and shred xml nodes '/a/i' it to rows using CROSS APPLY c.nodes('/a/i');
alias rows using U(d), so one 'i' maps to column d (e.g. 'my')
- d.value('.', 'nvarchar(max)') extracts node content and casts it to character type
I execute the following:
SELECT url FROM mytable WHERE 1
result: url: 'http://www.ciao.es/Epson_Stylus_S22__2007613'
Everything ok by now...but when I do:
SELECT * FROM mytable WHERE url = 'http://www.ciao.es/Epson_Stylus_S22__2007613'
I get nothing!!!
I tried using LIKE, changing the quotes, etc...
what am I doing wrong?
You are having ' ' in your table field url .So if you are querying you will get output like this:
'http://www.ciao.es/Epson_Stylus_S22__2007613' (with in single quote)
SO
Write in this way:
SELECT * FROM mytable WHERE url = "'http://www.ciao.es/Epson_Stylus_S22__2007613'"
Or remove single quote from table.
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';