如何使用 DBIx::Class 进行子选择?
我从 DBIx::Class 开始,我有一个想要在 DBIx::Class 中的子选择,但我很困惑并且无法构建代码。
我的 MySQL 选择是这样的:
Select name from tblCategory where id = (
Select id from tblCategory where id = (
Select id from tblRadio where name = "RFM"
)
);
我读到 DBIx::Class 不支持子选择;是这样吗?如果是这样,遇到这种情况你会怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据
DBIx::Class: :Manual::Cookbook
有一个新的子查询功能:它被标记为实验性的,所以 YMMV。
但还要注意
SQL::Abstract
其中DBIx::Class
在构建其查询时使用确实有一个新的使用 -nest 的子查询功能。According to the
DBIx::Class::Manual::Cookbook
there is a new Subquery feature:It is marked EXPERIMENTAL so YMMV.
However also note that
SQL::Abstract
whichDBIx::Class
uses when building its queries does have a new subquery feature using -nest.难道这不能表示为连接吗?
这假设您在名为“Radio”的类别中具有关系。如果您不这样做,有大量文档可以帮助您设置 关系并了解如何执行加入。
至于子查询,最新版本的说明书说它们是 支持,但是实验性。
Couldn't this be represented as a join?
This assumes you have a relationship in Category named 'Radio'. If you don't, there's plenty of documentation to help you setup relationships and learn how to perform joins.
As for subqueries, the cookbook for the most recent version says they are supported, but experimental.
那么,当您使用 DBIC 的
search()
方法时,您始终可以提供标量引用来插入文字 SQL。例如:当我需要比 DBIC 支持的开箱即用的更多表现力时,这就是我过去必须做的事情。但我不知道在这种情况下这是否是“正确”的做法。
Well, you can always supply a scalar reference to insert literal SQL when you're using DBIC's
search()
method. For example:That's what I've had to do in the past when I needed more expressiveness than DBIC supported out-of-the-box. I don't know, though, whether that's the "right" thing to do in this case.
经过与 DBIC 的一番战斗后,我最终获胜:P (是的!)
必须重写一些东西,并且必须忘记子选择并做好关系。
我知道这段代码并不代表最初的问题,但如果我必须再次重写全部内容,我将向您展示我的“项目”的另一部分,其中我又遇到了其他困难。
接下来是我所做的:
DBIx::Class Schema
与 Utilizadores(用户)有关系
NEXT(使其工作的脚本)
结果:
不是我真正想要的,但是最接近的,目标是仅选择我想要的列...也许我会达到该目标
after some fight with DBIC i win in the end :P (YEAH!)
Had to rewrite some stuff, and had to forget the subselects and done the relationships well.
I know that this code don't represent the inicial question, but if i had to rewrite all again, i'm showing you the other part of my "project", where i had again other dificulties.
Next is what i've done:
DBIx::Class Schema
Has a relationship with Utilizadores (users)
NEXT (the script to make it work)
The Result:
Wasn't what i really wanted, but is the nearest, the goal is to select only the columns i want... maybe i will reach that goal
子选择现在是实验性功能,如果您需要能够在编译时允许它们,则非常有用。然而,我发现以下是一个很好的方法:
Subselects are experimental feature now and useful if you need to be able to allow them at compile time. However I find the following to be a good approach: