SQL:通过子查询在同一列中搜索两个字符串
我有三张表使用Oracle数据库:
Recipe
RID Cocktail Made_by
1 Daiquiri Otto
2 Brooklyn Lamp Bene
3 Mai Tai Otto
4 Brooklyn Lamp Bene
5 Brooklyn Lamp Otto
6 Drivers Glow Alfred
Ingredients
Ing_ID Ing-Name From_Where
1 Obstgeist Deutschland
2 Kuba-Rum Cuba
3 Limettensaft Spain
4 Obstgeist Deutschland
5 Grapefruit Deutschland
6 Zitronensaft Spain
7 Jamaika-Rum Jamaika
8 Martinique Italy
9 Curacao Venezuela
10 Mandelsirup Italy
11 Grapefruit Spain
12 Brombeersirup Deutschland
Mix
RID Ing_ID Quantity
1 2 60
1 3 30
2 4 40
2 5 10
2 6 20
3 7 30
3 8 30
3 9 15
3 10 8
3 3 20
4 4 40
4 5 10
4 3 15
5 4 40
5 5 10
5 3 10
5 6 10
6 11 150
6 12 30
混合表互连食谱和成分。我应该找到一种由巴尔曼·奥托(Barman Otto)制作的鸡尾酒,它利用西班牙和古巴的成分。这是我到目前为止尝试的:
SELECT DISTINCT r.rid, r.cocktail
FROM recipe r
join mix m
on r.rid = m.rid
JOIN ingredients i
on m.ing_id = i.ing_id
WHERE r.made_by LIKE 'Otto' AND
i.ing_id IN (SELECT ing_id
FROM ingredients
WHERE
from_where LIKE 'Spain')
AND EXISTS (SELECT *
FROM ingredients
WHERE
from_where LIKE 'Cuba');
从此查询中预期的输出:
RID Cocktail
1 Daiquiri
目前,我只是在没有任何成分的情况下得到了Barman Otto制作的所有鸡尾酒,例如:
RID Cocktail
1 Daquiri
3 Mai Tai
5 Brooklyn Lamp
由于我是SQL的新手,我不知道如何建立同步查询,该查询同时搜索两个字符串或在同一字段中的两个记录。到目前为止,这是我最好的尝试。 每个提示都将不胜感激!非常感谢您
I have three tables using an Oracle database:
Recipe
RID Cocktail Made_by
1 Daiquiri Otto
2 Brooklyn Lamp Bene
3 Mai Tai Otto
4 Brooklyn Lamp Bene
5 Brooklyn Lamp Otto
6 Drivers Glow Alfred
Ingredients
Ing_ID Ing-Name From_Where
1 Obstgeist Deutschland
2 Kuba-Rum Cuba
3 Limettensaft Spain
4 Obstgeist Deutschland
5 Grapefruit Deutschland
6 Zitronensaft Spain
7 Jamaika-Rum Jamaika
8 Martinique Italy
9 Curacao Venezuela
10 Mandelsirup Italy
11 Grapefruit Spain
12 Brombeersirup Deutschland
Mix
RID Ing_ID Quantity
1 2 60
1 3 30
2 4 40
2 5 10
2 6 20
3 7 30
3 8 30
3 9 15
3 10 8
3 3 20
4 4 40
4 5 10
4 3 15
5 4 40
5 5 10
5 3 10
5 6 10
6 11 150
6 12 30
The mix table interconnects Recipe and Ingredients. I am supposed to find the one cocktail made by the barman Otto which utilizes ingredients from Spain and Cuba. This is what I have tried until now:
SELECT DISTINCT r.rid, r.cocktail
FROM recipe r
join mix m
on r.rid = m.rid
JOIN ingredients i
on m.ing_id = i.ing_id
WHERE r.made_by LIKE 'Otto' AND
i.ing_id IN (SELECT ing_id
FROM ingredients
WHERE
from_where LIKE 'Spain')
AND EXISTS (SELECT *
FROM ingredients
WHERE
from_where LIKE 'Cuba');
Expected Output from this query:
RID Cocktail
1 Daiquiri
At the moment, I am just getting all the cocktails made by the barman Otto without distinction of the ingredients, like:
RID Cocktail
1 Daquiri
3 Mai Tai
5 Brooklyn Lamp
Since I am new to SQL, I do not know how to build up a synchronized query which searches for two strings at the same time or for two records in the same field. That was by far my best attempt.
Every hint would be really appreciated! Thank you very much in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有了此查询,您将获得所有鸡尾酒,这些鸡尾酒具有来自古巴和西班牙的成分以及该鸡尾酒的成分,也可以来自其他国家 /地区,只要一个来自古巴,一种来自西班牙。
With this query you will get all the cocktails that have ingredients from Cuba and Spain and ingredients for that cocktails can also be from other countries as long as one is from Cuba and one is from Spain.
DEMO