Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Subselect with DBIx::Class?

I'm starting with DBIx::Class and i have a subselect that wanted to be in DBIx::Class, but i'm getting confused and can't build the code.

My MySQL select is this one:

Select name from tblCategory where id = (
    Select id from tblCategory where id = (
         Select id from tblRadio where name = "RFM"
    )
);

I read that DBIx::Class don't support subselect; is that true? If so, what do you do in situations like this?

like image 807
Davidslv Avatar asked Dec 23 '22 07:12

Davidslv


1 Answers

According to the DBIx::Class::Manual::Cookbook there is a new Subquery feature:

my $inside_rs = $schema->resultset('Radio')->search({ name => 'RFM' });

my $rs = $schema->resultset('Category')->search({
    id => { '=' => $inside_rs->get_column('id')->as_query },
});

It is marked EXPERIMENTAL so YMMV.

However also note that SQL::Abstract which DBIx::Class uses when building its queries does have a new subquery feature using -nest.

like image 128
draegtun Avatar answered Jan 02 '23 22:01

draegtun