Php Mysql:多个where查询
我有一张桌子,用来存储客户对产品的选择 它存储客户编号、产品编号以及是否对产品表示感谢。我们存储是/否是因为客户必须对所有产品进行选择,以便我们可以检查他们是否已做出所有选择Php Mysql:多个where查询,php,mysql,sql,Php,Mysql,Sql,我有一张桌子,用来存储客户对产品的选择 它存储客户编号、产品编号以及是否对产品表示感谢。我们存储是/否是因为客户必须对所有产品进行选择,以便我们可以检查他们是否已做出所有选择 Customer | Product | Status --------------------------- 12345 | 1 | 0 12345 | 2 | 1 12345 | 3 | 1 12345 | 4 | 0 12345 | 5
Customer | Product | Status
---------------------------
12345 | 1 | 0
12345 | 2 | 1
12345 | 3 | 1
12345 | 4 | 0
12345 | 5 | 1
23456 | 1 | 1
23456 | 2 | 0
23456 | 3 | 1
23456 | 4 | 1
23456 | 5 | 0
我想做的是检查哪些客户选择了一套特定的产品。 这可能类似于
select*from选项,其中product=1和product=3按客户分组
,但我还必须查询status=1的产品
有没有办法在查询中解决这个问题,或者我必须求助于对一些PHP的几个调用
select customer
from choices
where status = 1 and product in (1,3)
group by customer
having count(distinct product) = 2
这样做还可以让您在以后添加更复杂的逻辑:如果您希望所有在2012年购买产品1,在2013年购买产品2的人都可以在where子句中添加额外的条件来筛选这些条件
这样做还可以让您在以后添加更复杂的逻辑:如果您希望所有在2012年购买产品1、在2013年购买产品2的人都可以在where子句中添加额外的条件来筛选这些条件。Condition product=1和product=3将为您提供零行,因为乘积不能同时是1和3。应该注意这是一个错误的例子,更像是“whish it working this”。条件product=1和product=3会给你零行,因为乘积不能同时是1和3。应该注意这是一个错误的例子,更像是“whish it working this”.我想,
having count
就是我想要匹配的产品数量1,3,6,16,17
将=5
,对吗?我想,拥有count
将是我想要匹配的产品数量<代码>1,3,6,16,17将是=5
,对吗?
SELECT DISTINCT a.customer
FROM choices a, choices b
WHERE a.customer = b.customer
AND a.product = 1
AND b.product = 3;