You can use MySQL INNER JOIN to do it.
This script is going to help you with it:
SELECT customer_grid_flat.entity_id ,customer_grid_flat.name, customer_grid_flat.billing_company, company_erp.`is_end_user_agreement` FROM customer_grid_flat
INNER JOIN company ON company.company_name = customer_grid_flat.billing_company
INNER JOIN company_erp ON company_erp.company_id = company.entity_id;
Export SQL
To export this into a file you can run this code snippet below with the SQL file containing the script above:
mysql -h 127.0.0.1 -u magento -pmagento magento < customer-company-ua.sql > customer-company.csv
Convert it to CSV
To convert it to CSV separated by comma, you can run this command below instead.
mysql -h 127.0.0.1 -u magento -pmagento magento < customer-company-ua.sql | sed 's/\t/,/g' > customer-company.csv