Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export the data of a Mysql table including the values of referenced ID's of another table

//I have updated the question to hopefully clarify things better as I'm still at a loss. I got some very much appreciated help but I can't get it to work yet//

I'm still new at this, new here as well, so I hope I'll explain it clearly enough. If not, please let me know.

  1. I have 2 tables Table 1 "students" , Table 2 'Mixed data"

tables

  1. Table 1 "students" has the columns "ID", "Name" and "Experience"

    In the column "Experience" the fields refer to data from Table 2 "Mixed Data"

  2. I am able to export the data of the table "students" to CSV

I use for this export the following query

SELECT *
INTO OUTFILE '/tmp/results.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM students;
  1. In the file "results.csv" the data of the ID's will be the ID's and not the real value/name

I like to be able to export the data into CSV and have the values and not the ID reference in the exported file.

Current export result

ID | Name | Experience
1 | Gary | 5
2 | Mary | 4

the number "4" and "5" in Experience refers to the id in table 2 "Mixed data"

Result I like to have in the export .csv"

ID | Name | Experience
1 | Gary | Super experienced
2 | Mary | Lots of experience

In this export of the CSV the experience field contains now the real values, being "Super experience", "Lots of experience"

Is it possible to export the data of a table including the values of referenced ID's of another table and if yes, how should I adjust my query.

Thanks so much for any help.

like image 948
joanw Avatar asked Jan 19 '26 02:01

joanw


1 Answers

It is not a problem. Just add desired table to the SELECT statement, for example -

SELECT s.ID, s.Name, e.Experience FROM students s
JOIN experience e ON s.ID = e.ID
INTO OUTFILE '/tmp/results.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
like image 62
Devart Avatar answered Jan 21 '26 17:01

Devart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!