Details
- 
    Type:Bug 
- 
    Status: Done/Fixed
- 
    Priority:Trivial 
- 
    Resolution: Duplicate
- 
    Affects Version/s: 4.2.1
- 
    Fix Version/s: Unscheduled
- 
    Component/s: Core CiviCRM
- 
    Labels:None
- 
        Documentation Required?:None
- 
        Funding Source:Needs Funding
Description
When I have a custom search where the "select" clause starts with the word "Distinct", it run normally if I run it directly. However, if I try to save it as a smart group then it fails.
The problem seems to be in file CRM/Contact/BAO/GroupContactCache.php
  around line 344:  $insertSql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) ($selectSql);";
If I copy and paste the SQL from above, it starts out 
"INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) (SELECT 63 as group_id, DISTINCT contact_a.id as contact_id FROM civicrm_contact contact_a "...
If I remove the word "DISTINCT" from the statement, then it works fine.