Erlang:连接两个不在相等模式上的表

时间:2011-04-01 12:49:45

标签: join erlang inequality

有没有办法实现

SELECT *
FROM pattern p
  JOIN tag t ON t.tag LIKE CONCAT(p.pattern, '%') AND t.type = p.type

就两个问题而言的erlang qlc而言:

  1. [{{Pattern,Type},Id}]
  2. [{{Tag,Type},Id}]
  3. 予。即不等式模式含义标签以Pattern和Type = Type开头。

    提前致谢。

1 个答案:

答案 0 :(得分:2)

非常简单:

1> ets:new(a,[named_table]),[ets:insert(a,X) || X<-[{{"foo", bar},12},{{"baz", quux},11}]].
[true,true]
2> ets:new(b,[named_table]),[ets:insert(b,X) || X<-[{{"foobar", bar},12},{{"bazquux", quux},11},{{"fooxxx", bar},1},{{"bazbaz", baz},11}]].
[true,true,true,true]
3> qlc:e(qlc:q([{Pattern, Type1, Tag, Id1, Id2} || {{Pattern, Type1}, Id1} <- ets:table(a), {{Tag, Type2}, Id2} <- ets:table(b), Type1 =:= Type2, lists:prefix(Pattern, Tag)])).
[{"baz",quux,"bazquux",11,11},
 {"foo",bar,"foobar",12,12},
 {"foo",bar,"fooxxx",12,1}]