Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Unanswered: 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...

    No code has to be inserted here.


    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

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT GROUP_CONCAT(field_c SEPARATOR ' ') AS output
      INTO OUTFILE '/tmp/file.txt' 
      FROM table
     WHERE field_a = 1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    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

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •