Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to select limited rows randomly in DBIx::Class?

by phildeman (Scribe)
on Jan 28, 2015 at 22:08 UTC ( [id://1114821]=perlquestion: print w/replies, xml ) Need Help??

phildeman has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to query a database by randomly selecting limited rows. I know how to do it in SQL. I want to convert this:
SELECT column FROM table ORDER BY RAND() LIMIT 20;
to this:
my @tickers = $schema->resultset( 'TickerTable' )->search( {}, { order_by => Random(), + columns => ['DisplayName'], + rows => 20 + });

Replies are listed 'Best First'.
Re: How to select limited rows randomly in DBIx::Class?
by Your Mother (Archbishop) on Jan 28, 2015 at 23:25 UTC

    :P

    my @tickers = $schema ->resultset('TickerTable') ->search({}, { order_by => \"RAND()", columns => ['DisplayName'], rows => 20 });

    String refs (\"") are used as literals to the DB in this case.

Re: How to select limited rows randomly in DBIx::Class?
by duelafn (Parson) on Jan 29, 2015 at 02:20 UTC

    See also Helper::ResultSet::Random which would (I believe) give the lovely syntax:

    my @tickers = $schema->resultset( 'TickerTable' )->rand(20)->search({} +, { columns => ['DisplayName'] });

    Good Day,
        Dean

Re: How to select limited rows randomly in DBIx::Class?
by erix (Prior) on Jan 28, 2015 at 22:12 UTC

    Can you say what database (DBMS) this should work in?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1114821]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-29 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found