Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.72 KB | None | 0 0
  1. SELECT
  2.     customers.id AS customer_id,
  3.     customers.account_code AS customer_account_code,
  4.     customers.name AS customer_name,
  5.     customer_main_phone.number AS customer_main_phone,
  6.     customer_main_email.email_address AS customer_main_email,
  7.     customer_contacts.id AS contact_id,
  8.     IF(customer_contacts.is_primary,'Yes','No') AS main_contact,
  9.     IF(customer_contacts.accounting_contact,'Yes','No') AS accounting_contact,
  10.     customer_contacts.first_name AS contact_first_name,
  11.     customer_contacts.last_name AS contact_last_name,
  12.     customer_contacts.position AS contact_position,
  13.     contact_main_phone.number AS contact_primary_phone,
  14.     contact_secondary_phone.number AS contact_secondary_phone,
  15.     contact_main_email.email_address AS contact_primary_email,
  16.     contact_secondary_email.email_address AS contact_secondary_email
  17. FROM
  18.     customers
  19.         LEFT JOIN
  20.     customer_contacts ON customer_contacts.customer_id = customers.id
  21.         LEFT JOIN
  22.     phone_numbers AS customer_main_phone ON customer_main_phone.id = customers.phone_id
  23.         LEFT JOIN
  24.     phone_numbers AS contact_main_phone ON contact_main_phone.id = customer_contacts.phone_id
  25.         LEFT JOIN
  26.     phone_numbers AS contact_secondary_phone ON contact_secondary_phone.id = customer_contacts.phone_id
  27.         LEFT JOIN
  28.     email_addresses AS customer_main_email ON customer_main_email.id = customers.email_id
  29.         LEFT JOIN
  30.     email_addresses AS contact_main_email ON contact_main_email.id = customer_contacts.email_id
  31.         LEFT JOIN
  32.     email_addresses AS contact_secondary_email ON contact_secondary_email.id = customer_contacts.email_id
  33. WHERE
  34.     customers.deleted_at IS NULL
  35.         AND customer_contacts.deleted_at IS NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement