Created
January 21, 2013 04:22
-
-
Save moltar/4583580 to your computer and use it in GitHub Desktop.
Array of subqueries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT `me`.`id`, `me`.`parent_id`, `me`.`created`, `me`.`updated`, `me`.`type`, `me`.`name`, `me`.`contact_id` | |
FROM ( | |
SELECT `me`.`id`, `me`.`parent_id`, `me`.`created`, `me`.`updated`, `me`.`type`, `me`.`name`, `me`.`contact_id` | |
FROM `companies` `me` | |
WHERE `id` IN ( ( | |
SELECT `me`.`company_id` | |
FROM `user_roles` `me` | |
WHERE `user_id` = 'ARRAY(0x7fbb6072f818)' | |
GROUP BY `company_id` | |
), ( | |
SELECT `me`.`company_id` | |
FROM `user_actions` `me` | |
WHERE `user_id` = 'ARRAY(0x7fbb6072f338)' | |
GROUP BY `company_id` | |
) ) | |
) `me` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub companies { | |
my $self = shift; | |
my $user_id = $self->id; | |
my $roles = $self->schema->user_roles->search({ user_id => $user_id }, | |
{ group_by => 'company_id' })->get_column('company_id')->as_query; | |
my $actions = $self->schema->user_actions->search({ user_id => $user_id }, | |
{ group_by => 'company_id' })->get_column('company_id')->as_query; | |
return $self->schema->companies->search( | |
{ 'id' => { -in => [$roles, $actions] } })->as_subselect_rs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment