All Things Techie With Huge, Unstructured, Intuitive Leaps

A Couple of MySQL Tips - Join on Three Tables and Select Results into a File


These are a couple of things that I had to do with a mysql database lately and I thought that I would pass on these tips.

We were debugging a problem where certain data elements were dimensioned across three tables.  While debugging program logic, we wanted to see the data aggregated by the common column across all three tables.  Here is the basic sql statement to do that:

SELECT * (or any number of columns separated by commas) FROM table1 join table2 ON table1.primarykey = table2.foreignkey
join table3 ON table2.primarykey = table3.foreignkey;

OK, so once you do that, you want to put it into a separate file so that you can ftp it from the server to the dev platform to analyze the data.  Here is how you select results into a file:

SELECT * (or any number of columns separated by commas) FROM table1 INTO OUTFILE '/tmp/table1.txt';

Hope this helps someone.

No comments:

Post a Comment