0

我有一个使用以下记录创建的记忆表“用户”

-record(users,{username,nickname,age})

假设我有如下记录的 mnesia 表

users | username | nickname | age

users | John     | baddy    | 25 

users | Monk     | John     | 26

我有一个与表格匹配的名称...(说“John”)..我想选择或匹配具有“John”作为用户名或昵称的行...我怎样才能做到这一点?如果有人可以建议我一个方法,我会很高兴...

谢谢

4

3 回答 3

1

看看 QLC—— Learn You Some Erlang 这里有一个简单的例子,或者这里的手册页。您可以使用布尔值来匹配,例如(在伪代码中)(用户名 == John 或昵称 == John)。

于 2013-05-21T12:16:52.300 回答
1
-module(wy).
-compile(export_all).

-record(users, {username, nickname, age}).
-include_lib("stdlib/include/ms_transform.hrl").

create_table()->
    mnesia:create_schema(node()),
    mnesia:start(),
    mnesia:create_table(users, [{attributes, record_info(fields, users)}]).

test()->
    Fun = fun()->
              mnesia:write(#users{username = "John", nickname = "baddy", age = 25}),
              mnesia:write(#users{username = "DaDa", nickname = "KaKa", age = 24}),
              mnesia:write(#users{username = "Monk", nickname = "John", age = 24})
          end,
    mnesia:transaction(Fun),

    Fun2 = ets:fun2ms(fun({users, UserName, NickName, Age}) when
                       UserName == "John" orelse NickName == "John" ->
                            {users, UserName, NickName, Age}
                  end), 

    mnesia:transaction(fun() -> mnesia:select(users, Fun2) end).
于 2013-05-27T08:47:44.803 回答
0
选择(Q)->
    案例记忆:is_transaction() 的
        假->
            F = fun(QH)-> qlc:e(QH) 结束,
            mnesia:activity(transaction,F,[Q],mnesia_frag);
        真-> qlc:e(Q)
    结尾。

read_by_name_or_nickname(姓名)->
    数据 = select(qlc:q([X || X <- mnesia:table(users),
        ((X#users.username == Name) orelse (X#users.nickname == Name))])),
    数据。
于 2013-05-23T07:26:50.647 回答