我想知道是否有可能使用單個mysql查詢來完成某些操作,如果是這樣,那麼對最有效的方式感興趣做到這一點。SQL:返回具有給定項目的值的子集的項目的查詢
我有兩個字段的表...讓我們說配方和成分
我正在尋找具有給定配方的成分的子集所有的食譜。
爲了說明:
Recipe | Ingredient
-------------------
sandwich | bread
sandwich | lettuce
sandwich | mustard
sandwich | bacon
sandwich | tomato
bacon salad | lettuce
bacon salad | tomato
bacon salad | bacon
veggie salad | lettuce
veggie salad | tomato
veggie salad | cucumber
我傳遞「三明治」作爲參數,並需要一個查詢將返回培根沙拉(即成分都包含在夾層的列表成分),但不是蔬菜沙拉,因爲它含有黃瓜,這不在三明治中。
我查看了這個問題:
SQL query to exclude records that appear in other rows?
,但我認爲我的情況是不同的,更加複雜。如果我傳遞「培根」並排除所有包含「培根」以外的成分的食譜,這將是類似的,但我需要根據輸入配方成分的查詢生成我排除的內容列表。
希望有道理!我覺得這應該是相當平凡的,但我堅持。
TIA爲您提供幫助!
我不知道你是否能在MySQL中做到這一點,但在SQL Server中,爲連接到子選擇,或者使用/ CTE定義,這將是微不足道的表 – Nikki9696
謝謝,@ Nikki9696。你能爲我的例子建議一個SQLServer查詢嗎?我會看看它是否會與MySQL一起工作。 – Sofia