I've an mnesia table with fields say f1, f2, f3. If I would choose all of the rows using the area value as V1, I'd use mnesia:select and match specifications or perhaps a simple mnesia:match_object. Now I have to choose all of the rows that have V1, V2, V3 ... or Vn (a listing of arbitrary length) because the value for area f1. In SQL I'd make a move like

SELECT * FROM tablename WHERE f3 IN (V1, V2, V3, ... , Vn)

How do you do that in mnesia?

But for the match-spec fix for your problem, if QLC is measured as not capable enough.

> ets:fun2ms(fun ({_,X}=E) when X == a -> E end).

The ets:fun2ms is really a parse transform that may translate some funs into matchspec values. I personally use it as being a quick method of getting the matchspec I would like. We obtain a listing having a matchspec that finds a tuple in which the second element is really a. We are able to repeat it for other secrets.

So allows populate an ets table with something to do this on, then produce a matchspec that suits only products with second element as 'a' or 'c'. (I enter this

ets:new(foo, [named_table]).

ets:insert(foo, [{1,a},{2,b},{3,c},{4,d}]).

Vs = [a,c].

MS = [{{'_','$1'},[{'==','$1',V}],['$_']} || V <- Vs].

ets:select(foo, MS).

After I run i get:


Christian stated nice solution but it may be made tiny bit simpler

2> ets:fun2ms(fun ({_,a}=E) -> E end).             

so for you personally match you may make simpler match spec

4> ets:new(foo, [named_table]).
5> ets:insert(foo, [{1,a},{2,b},{3,c},{4,d}]).
6> Vs = [a,c].
7> MS = [{{'_',V},[],['$_']} || V <- Vs].                
8> ets:select(foo, MS).

If you want "complex" queries then QLC is really a succinct method to condition them. QLC is really a parse-transform where you can make use of the list comprehension syntax on various tables, including mnesia tables.

You must possess the following include, because it allows the parse-transform which make QLC possible within this source file.


The fundamental query form appears to be the next, developing a query-handle after which evaluating the query.

QH = qlc:q([X || X <- Xs]),

After that you can use http://www.erlang.org/doc/man/mnesia.html#table-1 to get a QLC table back-led to a mnesia table. So that your query might be implemented because the following:

Vs = [V1,V2,...Vn],
QH = qlc:q([X || X <- mnesia:table(tablename), 
                 lists:member(X#tablename.f3, Vs)]),

This involves a table-scan, that is not so efficient. For those who have a catalog around the f3-column then you may rather transform it around and query first for records with f3 = V1, then V2, etc, ... and merge the outcomes.


An alternate is to produce a quite complex match-spec from your listing of V-values and run mnesia:choose.