If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SELECT INTO OUTFILE help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-12, 20:16
jinxy_tj jinxy_tj is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
SELECT INTO OUTFILE help

Hi,

I am trying to output one field of a mysql database to a file but with a condition of another fields data.

i.e...

field_a field_b field_c
1 a Hello
1 b World
2 c Please
2 d Help


in the above table I want to output field_c which I know I do with the following:

Code:
SELECT field_c INTO OUTFILE '/tmp/file.txt' FROM table;
BUT... could my output file contain just field_c data but only if field_a = 1

so in this instance my output would be Hello World

Thanks in advance for any help
Reply With Quote
  #2 (permalink)  
Old 02-04-12, 20:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT GROUP_CONCAT(field_c SEPARATOR ' ') AS output
  INTO OUTFILE '/tmp/file.txt' 
  FROM table
 WHERE field_a = 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-04-12, 21:52
jinxy_tj jinxy_tj is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
Perfect, Thanks very much.

Output is a little different but easily fixed with the below code just for reference if anyone happens to find this useful:

Code:
tr ' ' '\n' < file.txt > output.file.txt
Thanks r937
Reply With Quote
  #4 (permalink)  
Old 02-04-12, 21:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
hold on a sec... you ~didn't~ want Hello World concatenated??

because that's sure what it looked like you wanted

try this instead...
Code:
SELECT field_c AS output
  INTO OUTFILE '/tmp/file.txt' 
  FROM table
 WHERE field_a = 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-04-12, 22:13
jinxy_tj jinxy_tj is offline
Registered User
 
Join Date: Feb 2012
Posts: 3
Thumbs up

Thanks r937,

Yes I can see that my post was mis-leading, thanks again for your help!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On