[英]How to search if multiple values exist in the table and all have the same value in another column?
I'm looking to do something that is probably very simple to do but I can't formulate it and I don't really get help from Google.我想做一些可能非常简单的事情,但我无法制定它,我也没有真正从谷歌那里得到帮助。 I basically have 3 tables in my db, one is
cocktails
(with id and value), one is ingredients
(with id and value too) and one is correspondences
(with id, cocktail ids and ingredient ids).我的数据库中基本上有 3 个表,一个是
cocktails
(有 id 和值),一个是ingredients
(也有 id 和值),一个是correspondences
(有 id、鸡尾酒 id 和成分 id)。 This table basically reports what ingredient all cocktails have.这张表基本上报告了所有鸡尾酒的成分。
Through a form I'm given an array of ingredient ids in a $_GET['ingredients']
variable.通过一个表单,我在
$_GET['ingredients']
变量中得到了一组成分 ID。
I want to make a query in which I search for cocktails that I have enough ingredients to make.我想进行查询,在其中搜索我有足够原料制作的鸡尾酒。
I'm very sorry this is not very clear but I can't find any simpler way to describe my issue.很抱歉,这不是很清楚,但我找不到任何更简单的方法来描述我的问题。 Can any of you help me?
你们有人能帮我吗?
I want to make a query in which I search for cocktails that I have enough ingredients to make.
我想进行查询,在其中搜索我有足够原料制作的鸡尾酒。
We can phrase the logic with not exists
: we want all cocktails that have no ingredients that do not belong to our list.我们可以用
not exists
来表达逻辑:我们想要所有不含不属于我们列表的成分的鸡尾酒。
In SQL:在 SQL 中:
select *
from cocktails c
where not exists (
select 1
from correspondences r
where
r.id_cocktail = c.id
and r.id_ingredient not in (?, ?, ?)
)
This assumes that a cocktail always has at least one dependent row in correspondences
.这假设鸡尾酒在
correspondences
关系中始终至少有一个相关行。
The question marks represent the input list of ingredients, that you need to pass from your application.问号代表您需要从应用程序传递的成分输入列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.