0

I'm using WHERE XXX IN (SQL), so (SQL) must select only one Column

In this case I'm select some customer_id from a group, and those customer only belongs to that group only

WHERE `id_customer` IN(
SELECT g.`id_customer` // this must select *only one* column
FROM ps_customer_group AS g
Group By g.`id_customer`
Having COUNT(g.`id_customer`) = 1
AND  g.`id_group`=3  // **- Unknown column 'g.id_group' in 'having clause'** 
)

the raw data look like this, btw this is not the result
enter image description here

3
  • First of all you are grouping by id_customer and searching for having count = 1. So it will return you more than one values. Commented Aug 24, 2013 at 4:59
  • nono, this is raw data not the result, the sql I run only display syntax error Commented Aug 24, 2013 at 5:03
  • I didn't understand completely, you want to know if the user belongs to only one group ? Commented Aug 24, 2013 at 5:06

2 Answers 2

2

Try this:

WHERE id_customer IN(

    select g.id_customer from
    ps_customer_group as g
    where g.id_group=3 -- That belongs to this group
    and g.id_customer in(

       SELECT g.id_customer
       FROM ps_customer_group AS g 
       Group By g.id_customer 
       Having COUNT(g.id_group) = 1 -- is his only group
    )

)

Here is a test

Sign up to request clarification or add additional context in comments.

Comments

2

In case you want to know if a customer belongs to one group (ID=3) only, you have to change your query:

select g.id_customer
  from ps_customer_group AS g 
 where g.id_customer in (
         select id_customer 
           from ps_customer_group 
          where id_group=3
       )
 Group By g.id_customer 
Having COUNT(distinct g.id_group) = 1

this will list all customers which belong to group #3 and to no other group.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.