问题
Situation is:- i have mnesia tuples like {"Groupid(Primary key)","groupname","grouptype","creatorid","adminid","Member_list"}.
Member_list="memberone@xyz,membertwo@xyz,memberthree@xyz".Now i want to extract all those rows in which membertwo@xyz exists.How to apply guard while selecting from mnesisa for this??Any pointers
Now after going through the approach given by sherif following error is appearing
in function checktable1:getRecords/1 (checktable1.erl, line 201)
37> checktable1:getRecords("a"). ** exception error: no match of right hand side value {aborted, {undef, [{strings,tokens,["a,b,c",","],[]}, {checktable1,exists,2, [{file,"checktable1.erl"},{line,203}]}, {checktable1,'-getRecords/1-fun-1-',7, [{file,"checktable1.erl"},{line,197}]}, {qlc,collect,1,[{file,"qlc.erl"},{line,1330}]}, {qlc,eval,2,[{file,"qlc.erl"},{line,296}]}, {mnesia_tm,apply_fun,3, [{file,"mnesia_tm.erl"},{line,833}]}, {mnesia_tm,execute_transaction,5, [{file,"mnesia_tm.erl"},{line,813}]}, {checktable1,getRecords,1, [{file,"checktable1.erl"},{line,201}]}]}} in function checktable1:getRecords/1 (checktable1.erl, line 201)
回答1:
you will have to use mnemosyne
getRecords(ListMember)->
F = fun() ->
Q = qlc:q(
[
Record
|| Record <- mnesia:table(table_name_here),
exists(Record#table_name_here.member_list, ListMember)
]),
qlc:e(Q)
end,
{atomic, L}=mnesia:transaction(F),
L.
you then need to implement function exists(Member_list, Member) which scans the Member_list for the member and returns true if found and false otherwise. dont forget to
-include_lib("stdlib/include/qlc.hrl").
This was not complied, its only for demonstration. Might i also suggest you change your database design to avoid flattened lists (lists in the form of strings), or any list for that matter as a value. if possible of course, i do not know what you`re doing. You should at least be able to put a list of members there rather than a string, This is mnesia you can put anything anywhere in any table. Does not mean you should though.
edit:
exists(ML, M)->lookUp(string:tokens(ML, ","), M).
lookUp([], M)->false;
lookUp([M|R], M)->true;
lookUp([_|R], M)->lookUp(R,M).
if you want you could also use the following instead of exists(Record#table_name_here.member_list, ListMember).
lists:member(ListMember, string:tokens(Record#table_name_here.member_list, ","))
来源:https://stackoverflow.com/questions/28759169/extracting-multiple-row-corresponding-to-a-value-in-mnesia