简体   繁体   English

MYSQL查找第二行具有给定值的所有行

[英]MYSQL find all rows where second row have given values

my database table looks like: 我的数据库表如下所示:

╔════╦═══════════╦═══════════╗
║ ID ║ RECIPE_ID ║   NAME    ║
╠════╬═══════════╬═══════════╣
║  1 ║        1  ║ Apple     ║
║  2 ║        2  ║ Apple     ║
║  3 ║        2  ║ Orange    ║
║  4 ║        3  ║ Kiwi      ║
║  5 ║        1  ║ Kiwi      ║
║  6 ║        3  ║ Cherry    ║
║  7 ║        3  ║ Banana    ║
╚════╩═══════════╩═══════════╝

When i'm querying mysql for "Apple" AND "Orange" , so i should get the RECIPE_ID 2 because "Apple" and "Orange" have the same RECIPE_ID or second example: 当我向mysql查询"Apple""Orange" ,我应该得到RECIPE_ID 2,因为"Apple""Orange"具有相同的RECIPE_ID或第二个示例:

When looking for "Kiwi" AND "Banana" i should get the RECIPE_ID 3 当寻找"Kiwi""Banana"我应该得到RECIPE_ID 3

Here is my SQL I have tried 这是我尝试过的SQL

SELECT recipe_id, name 
FROM foodtipps.rezepte_zutaten 
WHERE name='Kiwi' AS 'NAME1' AND 
name='Banana AS 'NAME2' GROUP BY recipe_id

Hope you understand my problem. 希望你理解我的问题。 Thank you! 谢谢!

If you have only two value for search you can use a inner join 如果只有两个搜索值,则可以使用内部联接

select a.recipe_id 
from my_table as a
inner join my_table as b on  a.recipe_id = b.recipe_id
where a.name ='Apple' 
and b.name ='Orange';

This can be extended to many more ingredients easily: 这可以轻松扩展到更多成分:

SELECT recipe_id
FROM theTable
WHERE name IN ('Apple', 'Orange')
GROUP BY recipe_id
HAVING COUNT(*) = 2 /* number of ingredients in the list */

The answer given by scaisEdge solves the problem you asked . scaisEdge给出的答案解决了您提出的问题

It's possible to extend this to support an indefinite number of ingredients, but its a bit messy adding a join to the table each time, hence... 可以扩展它以支持无限数量的成分,但是每次添加一个联接到表中都会有点混乱,因此...

 SELECT recipe_id, COUNT(DISTINCT name)
 FROM recipe
 WHERE name in (
     'Apple',
     'Orange',
      ...
 )
 GROUP BY recipe_id
 ORDER BY 1 DESC;

You might also add a HAVING COUNT(DISTINCT name)=xxx where xxx is your number of ingredients before the ORDER BY. 您还可以添加一个HAVING COUNT(DISTINCT name)=xxx ,其中xxx是ORDER BY之前您的配料数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mysql查询查找与另一行具有相同值的所有行 - Mysql query to find all rows that have the same values as another row 查找MySQL中没有相应第二行的行 - Find rows without corresponding second row in MySQL MySQL查找所有行,其中行数(对于列的可能值)小于n - MySQL find all rows where rows with number of rows for possible values of column is less than n Mysql:在table1中找到1行,它将table2中的多行与给定值连接起来 - Mysql: find 1 row in table1 that joins multiple rows in table2 with given values Mysql join,所有行都满足条件; 第二个查询 - 至少一行满足条件(部分匹配) - Mysql join where all rows meet condition; second query - where at least one row meet condition (partial match) 从mysql db中提取数据,其中用户有多行,但正在寻找具有两个最大值的行 - pulling data from mysql db where users have multiple rows but looking for the row with two maximum values MySQL查找在所有子记录中找到给定值的父记录 - Mysql find parent records where given values are found in all child records MYSQL:我想用另一个表中的值更新表中的所有行,其中第一个表中的值等于第二个表 - MYSQL: I want to update all rows in a table with values from another table where values from the first table are equal to the second MySQL:在行列表中使用给定值查找正确的行 - MySQL: in list of rows find a proper row using given value 即使指定了where子句,MySQL Trigger也会更新所有行 - MySQL Trigger updates all rows even if a distinct where clause is given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM