简体   繁体   English

如何搜索表中是否存在多个值并且在另一列中都具有相同的值?

[英]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.

相关问题 如何根据同一表中另一列的值获取列的所有值? - How to fetch all values of a column based on value of another column in same table? 如何检查同一ID在另一列中是否有多个值? - How to check same id have multiple values in another column? 如何从一个表中获取所有值,该表将自身包含在同一个表中的另一个值邻居字段? - How to get all values from a table which includes itself as another value neighbour field at the same table? MYSQL查询-一种基于同一表中另一列中的DISTINCT值从一列返回所有值的简单方法? - MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table? 如何将值与列名相同的多个表中的列值进行比较? - How to compare value with column values in multiple table whose column names are same? 如何从同一单个列和表中搜索两个元值 - How to search two meta values from same single column and table 如何根据 Laravel 8 中同一表中的另一列在列中存储值 - How to store value in column depending on another column in same table in Laravel 8 将列值复制到同一表中的另一列 - Copy column values to another column in the same table 如何检查所有行是否具有相同的列值? - How can I check if all rows have the same column value? 当列具有相同的值名称时如何自动运行rowspan表 - How to auto rowspan table when have column be same value name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM