SELECT DISTINCT columnAAA,name,bf1.columnBBB,bf2.columnBBB FROM table1 AS bt LEFT JOIN table_to_join AS bf1 ON bt.columnAAA=bf1.columnCCC LEFT JOIN table_to_join AS bf2 ON bt.columnAAA=bf2.columnDDD INTO OUTFILE '/var/lib/mysql-files/table1.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n';