AIM :将两个表(视一个表中的一个列而定,其中包含另一个表中另一列中提到的单词)结合起来。
表1 :
tibble::tribble(
~ORDER_ID, ~SUPERNAME_WITH_MODS, ~QUANTITY,
1, "Mods, , 2 Regular Fries, 2 Regular Fries (Mods),", 0,
2, "Tomatoes, Tomatoes (Toppings), Toppings, , Lettuce, Lettuce (Toppings)", 0,
3, "Chicken, Dirty Chicken Cheeseburger, Dirty Chicken Cheeseburger (Chicken),", 0,
4, "Garlic & Buttermilk Mayo Dip Pot, Garlic & Buttermilk Mayo Dip Pot (Dips)", 0,
5, "Garlic Bread Pizza, , Verdure, Verdure (Pizza),", 0,
6, "Skinny Fries (Salt) - Large, Skinny Fries (Salt) - Large (Add On Sides),", 0,
7, "CYOSalad Veg, , Green Beans, Green Beans (CYOSalad Veg)", 0,
8, "Little Five Guys Style, Little Five Guys Style (Fries)", 0,
9, "Chicken de Volaille (Mains), Chips, Chips (Sides)", 0,
10, "Modifiers, Medium, Medium (Modifiers),", 0
)
表2:
tibble::tribble(
~ingredient, ~contributor,
"beef", "beef",
"chili", "beef",
"chilli", "beef",
"pork", "pork",
"bacon", "pork",
"chicken", "chicken"
)
预期结果:
基于表1的列SUPERNAME_WITH_MODS
连接表1和表2,其中包含表2的ingredient
列中的任何单词。请注意,如果没有匹配项,它将返回NULL
。
我还想强调一下,它必须在任何大小写(大写或小写)之间都匹配整个单词。
tibble::tribble(
~ORDER_ID, ~SUPERNAME_WITH_MODS, ~QUANTITY, ~ingredient, ~contributor,
1, "Mods, , 2 Regular Fries, 2 Regular Fries (Mods),", 0, "NULL", "NULL",
2, "Tomatoes, Tomatoes (Toppings), Toppings, , Lettuce, Lettuce (Toppings)", 0, "NULL", "NULL",
3, "Chicken, Dirty Chicken Cheeseburger, Dirty Chicken Cheeseburger (Chicken),", 0, "chicken", "chicken",
4, "Garlic & Buttermilk Mayo Dip Pot, Garlic & Buttermilk Mayo Dip Pot (Dips)", 0, "NULL", "NULL",
5, "Garlic Bread Pizza, , Verdure, Verdure (Pizza),", 0, "NULL", "NULL",
6, "Skinny Fries (Salt) - Large, Skinny Fries (Salt) - Large (Add On Sides),", 0, "NULL", "NULL",
7, "CYOSalad Veg, , Green Beans, Green Beans (CYOSalad Veg)", 0, "NULL", "NULL",
8, "Little Five Guys Style, Little Five Guys Style (Fries)", 0, "NULL", "NULL",
9, "Chicken de Volaille (Mains), Chips, Chips (Sides)", 0, "NULL", "NULL",
10, "Modifiers, Medium, Medium (Modifiers),", 0, "NULL", "NULL"
)
问题,我知道这将是左联接,但不确定在联接的ON
部分中应该写什么。
答案 0 :(得分:1)
不确定存在多个匹配项时如何处理。如果需要,您可能想添加一个GROUP BY
子句。
SELECT * FROM t1 LEFT JOIN t2 ON CONTAINS(LOWER(t1.supername_with_mods), LOWER(t2.ingredient));