Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT
- customers.id AS customer_id,
- customers.account_code AS customer_account_code,
- customers.name AS customer_name,
- customer_main_phone.number AS customer_main_phone,
- customer_main_email.email_address AS customer_main_email,
- customer_contacts.id AS contact_id,
- IF(customer_contacts.is_primary,'Yes','No') AS main_contact,
- IF(customer_contacts.accounting_contact,'Yes','No') AS accounting_contact,
- customer_contacts.first_name AS contact_first_name,
- customer_contacts.last_name AS contact_last_name,
- customer_contacts.position AS contact_position,
- contact_main_phone.number AS contact_primary_phone,
- contact_secondary_phone.number AS contact_secondary_phone,
- contact_main_email.email_address AS contact_primary_email,
- contact_secondary_email.email_address AS contact_secondary_email
- FROM
- customers
- LEFT JOIN
- customer_contacts ON customer_contacts.customer_id = customers.id
- LEFT JOIN
- phone_numbers AS customer_main_phone ON customer_main_phone.id = customers.phone_id
- LEFT JOIN
- phone_numbers AS contact_main_phone ON contact_main_phone.id = customer_contacts.phone_id
- LEFT JOIN
- phone_numbers AS contact_secondary_phone ON contact_secondary_phone.id = customer_contacts.phone_id
- LEFT JOIN
- email_addresses AS customer_main_email ON customer_main_email.id = customers.email_id
- LEFT JOIN
- email_addresses AS contact_main_email ON contact_main_email.id = customer_contacts.email_id
- LEFT JOIN
- email_addresses AS contact_secondary_email ON contact_secondary_email.id = customer_contacts.email_id
- WHERE
- customers.deleted_at IS NULL
- AND customer_contacts.deleted_at IS NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement