我正在使用WHERE XXX IN(SQL),因此(SQL)必须只选择一个列
在这种情况下,我从一个组中选择一些customer_id
,而这些客户只属于该组
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'**
)
原始数据看起来像这样,顺便说一下这不是结果
答案 0 :(得分:2)
试试这个:
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
)
)
这是test
答案 1 :(得分:2)
如果您想知道某个客户是否只属于一个组(ID = 3),您必须更改您的查询:
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
这将列出属于组#3且不属于其他组的所有客户。