根据存储在其他数据库中的已知变量拆分行

Splitting a row depending of known variable stored in an other database

我在 SAS,所以代码不纯 MySQL。

我有一个table这样的

ID     Fruits
1      Apple-Water-melon
2      Pine-Apple-Kiwi

还有另外一个水果各不相同

ID     Fruits
x      Apple
x      Kiwi
x      Pine-apple
x      Water-melon

我怎样才能有这样的决赛 table?

ID    Fruits
1     Apple
1     Water-melon
2     Pine-apple
2     Kiwi

是否可以解析第一个 table 并拆分变量(如果它们与第二个 table 中找到的匹配)?

谢谢,

您可以使用 FINDW() 函数检查 FRUIT 值是否出现在 FRUITS 列表中。

data have ;
  input ID Fruits .;
cards;
1      Apple-Water-melon
2      Pine-Apple-Kiwi
;

data list;
  fruitid +1 ;
  input fruit . ;
cards;
Apple
Kiwi
Pine-apple
Water-melon
;

proc sql ;
  create table want as
  select a.*,b.* 
  from have a 
    left join list b
    on 0 ne findw(Fruits,fruit,'-','ti')
  ;
quit;

结果

Obs    ID         Fruits          fruitid    fruit

 1      1    Apple-Water-melon       1       Apple
 2      2    Pine-Apple-Kiwi         1       Apple
 3      2    Pine-Apple-Kiwi         2       Kiwi
 4      2    Pine-Apple-Kiwi         3       Pine-apple
 5      1    Apple-Water-melon       4       Water-melon

嵌在PINE-APPLE中间的APPLE要如何消除匹配?是不是先优先取多个单词的水果名,再把那些水果从水果列表字符串中剔除?