MYSQL查询帮助(EAV表)
我有以下查询来检索对特定问题回答“是”“或”对另一个问题回答“否”的客户。
SELECT customers.id
FROM customers, responses
WHERE (
(
responses.question_id = 5
AND responses.value_enum = 'YES'
)
OR (
responses.question_id = 9
AND responses.value_enum = 'NO'
)
)
GROUP BY customers.id
效果很好。但是,我希望更改查询以检索对特定问题回答“是”“AND”对另一个问题回答“否”的客户。
关于如何实现这一目标有什么想法吗?
PS - 上表的响应采用 EAV 格式,即。行代表属性而不是列。
I have the following query to retrieve customers who answer YES to a particular question "OR" NO to another question.
SELECT customers.id
FROM customers, responses
WHERE (
(
responses.question_id = 5
AND responses.value_enum = 'YES'
)
OR (
responses.question_id = 9
AND responses.value_enum = 'NO'
)
)
GROUP BY customers.id
Which works fine. However I wish to change the query to retrieve customers who answer YES to a particular question "AND" answer NO to another question.
Any ideas on how I can achieve this?
PS - The responses above table is in an EAV format ie. a row represents an attribute rather than a column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的响应表中有一个名为 customer_id 的列。尝试将响应表连接到自身:
I'm assuming that you have a column called customer_id in your responses table. Try joining the responses table to itself:
大约是这样的:
我对缺少的
join
条件做了假设,修改为适合您的架构。Approximately as such:
I made an assumption on the missing
join
condition, modify as correct for your schema.