如何为Erlang mnesia写“in”查询?

时间:2017-07-06 17:44:37

标签: database erlang mnesia

我有一张mnesia表,让我们说employee。我需要找到名称在EmployeeNameList = ["Erlich", "Richard", "Gilfoyle", "Dinesh"]的所有员工记录。有没有办法使用mnesia:select或其他功能?

1 个答案:

答案 0 :(得分:1)

遵循Mnesia的文档 它可以按如下方式完成:

get_employees_by_name(NameList) -> 
  MatchHead = #employee{name = '$1', _ = '_'},
  Result = '$_'
  MatchSpec = [ { MatchHead, [{'=:=', '$1', Name}], [Result]} || Name <- NameList ],
  F = fun() -> 
     mnesia:select(employee, MatchSpec)
  end,
  {atomic, Result} = mnesia:transaction(F),
  Result.