Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    2

    Mysql 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

  2. #2
    rrl
    rrl is offline
    Just Joined!
    Join Date
    May 2011
    Location
    NJ
    Posts
    56
    Code:
    SELECT field_c INTO OUTFILE '/tmp/file.txt' FROM table WHERE field_a=1;

  3. #3
    Just Joined!
    Join Date
    Feb 2012
    Posts
    2

    Smile

    Just an update on this in case anyone else is looking for an answer.

    The code required to do this is

    Code:
    SELECT GROUP_CONCAT(field_c SEPARATOR ' ') AS output
      INTO OUTFILE '/tmp/file.txt' 
      FROM table
     WHERE field_a = 1
    It has made the output file a little messy but easily fixed with this:

    Code:
    tr ' ' '\n' < file.txt > output.file.txt
    Thanks to those who looked anyway

Posting Permissions

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