使用 dbix-class 加入子查询?
给定两个表:(传入的大大减少/简化的示例显示了关键问题)
app_data代表可以订阅的应用程序
id app_name
1 apple
2 berry
3 cherry
app_sub将电子邮件地址映射到
id email
1 alex
2 bob
2 coby
我想要的 应用程序从单用户的角度生成一个表,显示当前用户订阅了哪些应用程序,没有订阅哪些应用程序。
例如,从 Alex 的角度来看,我想得到:
desired_table
id app_name is_subscribed
1 apple true
2 berry false
3 cherry false
下面的纯 SQL 查询似乎没问题:
select id, app_name, email
from app_data left join ( select *
from app_sub
where email='alex'
) as subquery
on app_name.id=app_data.id;
但是我很难让它在 dbix 类中工作。
或者,我尝试像这样消除子查询:
$app_data_resultset->search( { -or => [ { email => 'alex' },
{ email => undef },
],
},
{ select => [ qw{ me.id
me.app_name
app_sub.email
},
],
as => [ qw{ id
app_name
email
},
],
join => 'app_sub',
);
但是,这(现在是预期的)会产生以下结果(在将 0 和 null 都视为 false 后):
bad_table
id app_name is_subscribed
1 apple true
3 cherry false
由于 'bob' 和 'coby' 是订阅了 id 2,where 子句完全消除了第二个 id。
任何帮助将不胜感激!
Given two tables: (incoming greatly reduced/simplified example that exhibits the key problem)
app_data represents applications that can be subscribed to
id app_name
1 apple
2 berry
3 cherry
app_sub maps email addresses to applications
id email
1 alex
2 bob
2 coby
I'd like to generate a table from a single-user point-of-view showing which applications the current user is subscribed to and not.
For example, from Alex's perspective I'd like to get:
desired_table
id app_name is_subscribed
1 apple true
2 berry false
3 cherry false
The following pure SQL query seems to be fine:
select id, app_name, email
from app_data left join ( select *
from app_sub
where email='alex'
) as subquery
on app_name.id=app_data.id;
However I'm having great difficulty getting that to work in dbix-class.
Alternatively, I tried to eliminate the subquery like so:
$app_data_resultset->search( { -or => [ { email => 'alex' },
{ email => undef },
],
},
{ select => [ qw{ me.id
me.app_name
app_sub.email
},
],
as => [ qw{ id
app_name
email
},
],
join => 'app_sub',
);
However, this (now expectedly) results in the following (after treating both 0 and null as false):
bad_table
id app_name is_subscribed
1 apple true
3 cherry false
Since 'bob' and 'coby' are subscribed to id 2, the where clause completely eliminates the second id.
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己做了这件事,没有使用 100% 任意 SQL,因为在我看来,这有点糟糕。相反,我所做的是将所述子查询插入到一些原始 SQL 中,然后使用 that 生成更多查询。这是我的示例(来自 https:/ /github.com/frioux/drinkup/blob/master/lib/DU/Schema/ResultSet/Drink.pm#L164):
如果我在某个时候有时间,我会筛选这个一个更容易理解的示例,但要点是我使用 DBIx::Class 生成硬查询并手动将它们粉碎在一个小点中,然后将其包装在子查询中,以便我可以使用普通的 DBIx::Class用于更多搜索的结果集方法。
I did this myself without going 100% Arbitrary SQL because IMO that kinda sucks. What I did instead was insert said subqueries into some raw SQL, and then use that to generate more queries. So here's my example (from https://github.com/frioux/drinkup/blob/master/lib/DU/Schema/ResultSet/Drink.pm#L164):
If I get some time at some point I'll winnow this down to a more digestible example, but the point is that I use DBIx::Class to generate the hard queries and manually smash them together in one small spot, and then wrap THAT in a subquery so that I can just use plain DBIx::Class resultset methods for more searches.
文森特,我现在正在尝试自己加入子查询。到目前为止我找不到任何人成功。
另一种方法是使用 $c->model('blah')->storage->dbh->prepare("query") 或“通过自定义 ResultSource 的任意 SQL”部分在普通 SQL 中执行此操作DBIx Class Cookbook 的内容,这似乎也是一种合理的方式。
Vincent, I am trying to join to a subquery now myself. I cannot find anyone succeeding so far.
An alternative is to just do it in plain SQL with $c->model('blah')->storage->dbh->prepare("query") OR the 'Arbitrary SQL through a custom ResultSource' section of the DBIx Class Cookbook, which also seems like a reasonable way.