CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Error creating smart group from custom search
Pages: [1]

Author Topic: Error creating smart group from custom search  (Read 1780 times)

StigMil

  • Guest
Error creating smart group from custom search
September 26, 2009, 06:34:44 am
Hi,

I have created a custom search, which seems to work and delivers the expected results.  However when I select all result records and try to create a smart group, I get a DB error referring to a field that is not part of my query.

The SQL query is:
Code: [Select]
SELECT
           DISTINCT (c.id) as contact_id,
           c.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact c
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = c.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id

The error message is
Quote
DB Error: no such field
Database Error Code: Unknown column 'contact_a.id' in 'IN/ALL/ANY subquery', 1054

Error Details:

Array
(
    [callback] => Array
        (
           
  • => CRM_Core_Error
  • [1] => handle
            )

       
[c*de] => -19
    [message] => DB Error: no such field
    [mode] => 16
    [debug_info] =>
            SELECT
           DISTINCT (c.id) as contact_id,
           c.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact c
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = c.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id
           
            WHERE  c.contact_type = 'Household'  AND cGroup.group_id = 6 AND cGroup.status = 'Added'
            GROUP BY house
            HAVING cl < cp
             AND contact_a.id NOT IN (
                              SELECT contact_id FROM civicrm_group_contact
                              WHERE civicrm_group_contact.status = 'Removed'
                              AND   civicrm_group_contact.group_id = 18 )  UNION
SELECT contact_id as contact_id
FROM   civicrm_group_contact
WHERE  civicrm_group_contact.status = 'Added'
  AND  civicrm_group_contact.group_id = 18  [nativecode=1054 ** Unknown column 'contact_a.id' in 'IN/ALL/ANY subquery']
    [type] => DB_Error
    [user_info] =>
            SELECT
           DISTINCT (c.id) as contact_id,
           c.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact c
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = c.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id
           
            WHERE  c.contact_type = 'Household'  AND cGroup.group_id = 6 AND cGroup.status = 'Added'
            GROUP BY house
            HAVING cl < cp
             AND contact_a.id NOT IN (
                              SELECT contact_id FROM civicrm_group_contact
                              WHERE civicrm_group_contact.status = 'Removed'
                              AND   civicrm_group_contact.group_id = 18 )  UNION
SELECT contact_id as contact_id
FROM   civicrm_group_contact
WHERE  civicrm_group_contact.status = 'Added'
  AND  civicrm_group_contact.group_id = 18  [nativecode=1054 ** Unknown column 'contact_a.id' in 'IN/ALL/ANY subquery']
    [to_string] => [db_error: message="DB Error: no such field" code=-19 mode=callback callback=CRM_Core_Error::handle prefix="" info="
            SELECT
           DISTINCT (c.id) as contact_id,
           c.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact c
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = c.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id
           
            WHERE  c.contact_type = 'Household'  AND cGroup.group_id = 6 AND cGroup.status = 'Added'
            GROUP BY house
            HAVING cl < cp
             AND contact_a.id NOT IN (
                              SELECT contact_id FROM civicrm_group_contact
                              WHERE civicrm_group_contact.status = 'Removed'
                              AND   civicrm_group_contact.group_id = 18 )  UNION
SELECT contact_id as contact_id
FROM   civicrm_group_contact
WHERE  civicrm_group_contact.status = 'Added'
  AND  civicrm_group_contact.group_id = 18  [nativecode=1054 ** Unknown column 'contact_a.id' in 'IN/ALL/ANY subquery']"]
)

In the error message, my original SQL query seems to have been extended by an additional AND, from "AND contact_a.id NOT IN ...".  I'm guessing this is something to do with the caching of searches for smart groups, but why does the error occur?

In fact the smart group does get saved but I then get the same error when I try to use the group - for instance through the "Find Contacts" form.

Thanks for any tips.

StigMil

  • Guest
Re: Error creating smart group from custom search
September 26, 2009, 06:46:25 am
Oops - I truncated the SQL query in my original post: the full query is
Code: [Select]
            SELECT
           DISTINCT (c.id) as contact_id,
           c.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact c
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = c.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id
           
            WHERE  c.contact_type = 'Household'  AND cGroup.group_id = 6 AND cGroup.status = 'Added'
            GROUP BY house
            HAVING cl < cp

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Error creating smart group from custom search
September 26, 2009, 11:09:28 am

can u alias your civicrm_contact table to contact_a

i.e.

Code: [Select]
FROM civicrm_contact contact_a
...

We need to fix the documentation to reflect this (will do so later tonite)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

StigMil

  • Guest
Re: Error creating smart group from custom search
September 27, 2009, 07:43:54 am
Yes, that did solve the problem - many thanks - but ... I now have a different error as below.  Apologies if this is just SQL ignorance, but it seems it must be a by-product of the UNION that is added by the caching  ..?

Thanks for any pointers ...

Quote
DB Error: unknown error

Database Error Code: The used SELECT statements have a different number of columns, 1222

Return to home page.

Error Details:

Array
(
    [callback] => Array
        (
           
  • => CRM_Core_Error
  • [1] => handle
            )

       
[c*de] => -1
    [message] => DB Error: unknown error
    [mode] => 16
    [debug_info] =>
            SELECT
           contact_a.id as contact_id,
           contact_a.household_name as house,
           COUNT(cR.contact_id_a) as cp,
           COUNT(cDate.date_left_eco_10) as cl
           
            FROM   
        civicrm_contact contact_a
        RIGHT JOIN civicrm_relationship cR ON cR.contact_id_b = contact_a.id
        RIGHT JOIN civicrm_value_player_info_1 cDate ON cDate.entity_id = cR.contact_id_a
        LEFT JOIN civicrm_group_contact cGroup ON cGroup.contact_id = cDate.entity_id
           
            WHERE  contact_a.contact_type = 'Household'  AND cGroup.group_id = 6 AND cGroup.status = 'Added'
            GROUP BY house
            HAVING cl < cp
             AND contact_a.id NOT IN (
                              SELECT contact_id FROM civicrm_group_contact
                              WHERE civicrm_group_contact.status = 'Removed'
                              AND   civicrm_group_contact.group_id = 22 )  UNION
SELECT contact_id as contact_id
FROM   civicrm_group_contact
WHERE  civicrm_group_contact.status = 'Added'
  AND  civicrm_group_contact.group_id = 22  [nativecode=1222 ** The used SELECT statements have a different number of columns]

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Error creating smart group from custom search
September 27, 2009, 12:41:11 pm

To save custom search as a smart group, you need to implement the contactIDs( ) function

the "select" clause from that sql should be just retrieve the contactID. We'll re-read the fix the documentation for 3.0 soon

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

StigMil

  • Guest
Re: Error creating smart group from custom search
September 27, 2009, 01:26:23 pm
Great stuff, will experiment with that. Many thanks.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Error creating smart group from custom search

This forum was archived on 2017-11-26.