I want to export all contacts with all field values through SQL.

I'm not able to export from CiviCRM, it throwing 504 Gateway timeout error.

What will be the SQL query?


It is little difficult to provide sql for contact export as it is has many tables joins. The basic query to fetch all primary details is pasted below. The output may not be same as what you get in exported file because some of the options are converted from value to label using php and also the query is not joined with the custom data table as the table name may vary because they are generated dynamically.

SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, contact_a.display_name as `display_name`, contact_a.do_not_email as `do_not_email`, contact_a.do_not_phone as `do_not_phone`, contact_a.do_not_mail as `do_not_mail`, contact_a.do_not_sms as `do_not_sms`, contact_a.do_not_trade as `do_not_trade`, contact_a.is_opt_out as `is_opt_out`, contact_a.legal_identifier as `legal_identifier`, contact_a.external_identifier as `external_identifier`, contact_a.nick_name as `nick_name`, contact_a.legal_name as `legal_name`, contact_a.image_URL as `image_URL`, contact_a.preferred_communication_method as `preferred_communication_method`, contact_a.preferred_language as `preferred_language`, contact_a.preferred_mail_format as `preferred_mail_format`, contact_a.hash as `hash`, contact_a.source as `contact_source`, contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_name`, contact_a.prefix_id as `prefix_id`, contact_a.suffix_id as `suffix_id`, contact_a.formal_title as `formal_title`, contact_a.communication_style_id as `communication_style_id`, contact_a.email_greeting_id as email_greeting_id, contact_a.postal_greeting_id as postal_greeting_id, contact_a.addressee_id as addressee_id, contact_a.job_title as `job_title`, contact_a.gender_id as `gender_id`, contact_a.birth_date as `birth_date`, contact_a.is_deceased as `is_deceased`, contact_a.deceased_date as `deceased_date`, contact_a.household_name as `household_name`, IF ( contact_a.contact_type = 'Individual', NULL, contact_a.organization_name ) as organization_name, contact_a.sic_code as `sic_code`, contact_a.user_unique_id as `user_unique_id`, contact_a.employer_id as `current_employer_id`, contact_a.is_deleted as `contact_is_deleted`, contact_a.created_date as `created_date`, contact_a.modified_date as `modified_date`, contact_a.addressee_display as addressee_display, contact_a.addressee_custom as addressee_custom, contact_a.email_greeting_display as email_greeting_display, contact_a.email_greeting_custom as email_greeting_custom, contact_a.postal_greeting_display as postal_greeting_display, contact_a.postal_greeting_custom as postal_greeting_custom, IF ( contact_a.contact_type = 'Individual', contact_a.organization_name, NULL ) as current_employer, civicrm_address.id as address_id, civicrm_location_type.id as location_type_id, civicrm_location_type.name as `location_type`, civicrm_address.street_address as `street_address`, civicrm_address.street_number as `street_number`, civicrm_address.street_number_suffix as `street_number_suffix`, civicrm_address.street_name as `street_name`, civicrm_address.street_unit as `street_unit`, civicrm_address.supplemental_address_1 as `supplemental_address_1`, civicrm_address.supplemental_address_2 as `supplemental_address_2`, civicrm_address.supplemental_address_3 as `supplemental_address_3`, civicrm_address.city as `city`, civicrm_address.postal_code_suffix as `postal_code_suffix`, civicrm_address.postal_code as `postal_code`, civicrm_address.geo_code_1 as `geo_code_1`, civicrm_address.geo_code_2 as `geo_code_2`, civicrm_address.manual_geo_code as `manual_geo_code`, civicrm_address.name as `address_name`, civicrm_address.master_id as `master_id`, civicrm_address.county_id as county_id, civicrm_address.state_province_id as state_province_id, civicrm_address.country_id as country_id, civicrm_phone.id as phone_id, civicrm_phone.phone_type_id as `phone_type_id`, civicrm_phone.phone as `phone`, civicrm_phone.phone_ext as `phone_ext`, civicrm_email.id as email_id, civicrm_email.email as `email`, civicrm_email.on_hold as `on_hold`, civicrm_email.is_bulkmail as `is_bulkmail`, civicrm_email.signature_text as `signature_text`, civicrm_email.signature_html as `signature_html`, civicrm_im.id as im_id, civicrm_im.provider_id as `im_provider`, civicrm_im.provider_id as provider_id, civicrm_im.name as `im`, civicrm_openid.id as openid_id, civicrm_openid.openid as `openid`, civicrm_worldregion.id as worldregion_id, civicrm_worldregion.name as `world_region`, civicrm_website.id as website_id, civicrm_website.url as `url`, 
                GROUP_CONCAT(DISTINCT IF(civicrm_group_contact.status = 'Added', civicrm_group_contact.group_id, '')),
                GROUP_CONCAT(DISTINCT civicrm_group_contact_cache.group_id)
              as groups, GROUP_CONCAT(DISTINCT(civicrm_tag.name)) as tags, GROUP_CONCAT(DISTINCT(civicrm_note.note)) as notes
FROM civicrm_contact contact_a   
LEFT JOIN civicrm_address 
  ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )  
LEFT JOIN civicrm_country 
  ON ( civicrm_address.country_id = civicrm_country.id )  
LEFT JOIN civicrm_email 
  ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)  
LEFT JOIN civicrm_phone 
  ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)  
LEFT JOIN civicrm_im 
  ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1)  
LEFT JOIN civicrm_openid 
  ON ( civicrm_openid.contact_id = contact_a.id AND civicrm_openid.is_primary = 1 )  
LEFT JOIN civicrm_location_type 
  ON civicrm_address.location_type_id = civicrm_location_type.id  
LEFT JOIN civicrm_group_contact 
  ON contact_a.id = civicrm_group_contact.contact_id  
LEFT JOIN civicrm_group_contact_cache 
  ON contact_a.id = civicrm_group_contact_cache.contact_id  
LEFT JOIN civicrm_entity_tag 
  ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND civicrm_entity_tag.entity_id = contact_a.id )  
LEFT JOIN civicrm_note 
  ON ( civicrm_note.entity_table = 'civicrm_contact' AND contact_a.id = civicrm_note.entity_id )  
LEFT JOIN civicrm_worldregion 
  ON civicrm_country.region_id = civicrm_worldregion.id  
LEFT  JOIN civicrm_tag 
  ON civicrm_entity_tag.tag_id = civicrm_tag.id  
LEFT JOIN civicrm_website ON contact_a.id = civicrm_website.contact_id  
WHERE (contact_a.is_deleted = 0) AND contact_a.is_deleted != 1     
    GROUP BY contact_a.id LIMIT 0, 100000

If you getting 504 error while doing export it worth to export in batches rather doing all in one go.



  • Thank you Pradeep. This query worked :) As you mentioned I tried to export contacts via batched but it throwing the same 504 error for some reason, even a specific group contains only 5 contacts. – Ashwin Jan 11 '20 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.