需要 SQL 和 Sequel 方面的帮助,涉及内部联接和 where/filter

发布于 2024-10-08 07:37:48 字数 1152 浏览 2 评论 0原文

需要帮助将 sql 转移到后续:
SQL:

SELECT table_t.curr_id FROM table_t
INNER JOIN table_c ON table_c.curr_id = table_t.curr_id 
INNER JOIN table_b ON table_b.bic = table_t.bic
WHERE table_c.alpha_id = 'XXX' AND table_b.name='Foo';

我被困在续集中,我不知道如何过滤,到目前为止是这样的:

 cid= table_t.select(:curr_id).
                    join(:table_c, :curr_id=>:curr_id).
                    join(:table_b, :bic=>:bic).
                    filter( ????? )  

用比上面更好的习语回答也很感激。Tnx。

更新:
我必须进行一些修改才能使其

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id).
  join(:table_b, :bic=>:table_t__bic). #add table_t or else ERROR: column table_c.bic does not exist
  filter(:table_c__alpha_id => 'XXX',
         :table_b__name => 'Foo')

在没有过滤器的情况下工作,

cid = DB[:table_t].select(:table_t__curr_id).
                    join(:table_c, :curr_id=>:curr_id, :alpha_id=>'XXX').
                    join(:table_b, :bic=>:table_t__bic, :name=>'Foo')

顺便说一句我使用 pgsql 9.0

Need help transfer sql to sequel:

SQL:

SELECT table_t.curr_id FROM table_t
INNER JOIN table_c ON table_c.curr_id = table_t.curr_id 
INNER JOIN table_b ON table_b.bic = table_t.bic
WHERE table_c.alpha_id = 'XXX' AND table_b.name='Foo';

I'm stuck in the sequel, I don't know how to filter, so far like this:

 cid= table_t.select(:curr_id).
                    join(:table_c, :curr_id=>:curr_id).
                    join(:table_b, :bic=>:bic).
                    filter( ????? )  

Answer with better idiom than above is appreciated as well.Tnx.

UPDATE:

I have to modify a little to make it works

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id).
  join(:table_b, :bic=>:table_t__bic). #add table_t or else ERROR: column table_c.bic does not exist
  filter(:table_c__alpha_id => 'XXX',
         :table_b__name => 'Foo')

without filter,

cid = DB[:table_t].select(:table_t__curr_id).
                    join(:table_c, :curr_id=>:curr_id, :alpha_id=>'XXX').
                    join(:table_b, :bic=>:table_t__bic, :name=>'Foo')

btw I use pgsql 9.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-10-15 07:37:48

这是纯粹的 Sequel 方式:

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id).
  join(:table_b, :bic=>:bic).
  filter(:table_c__alpha_id => 'XXX',
         :table_b__name => 'Foo')

请注意,您也可以在没有 WHERE 的情况下执行此操作,因为您使用的是 INNER JOIN:

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id, :alpha_id=>'XXX').
  join(:table_b, :bic=>:bic, :name=>'Foo')

This is the pure Sequel way:

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id).
  join(:table_b, :bic=>:bic).
  filter(:table_c__alpha_id => 'XXX',
         :table_b__name => 'Foo')

Note that you can also do this without a WHERE, since you are using INNER JOIN:

cid = DB[:table_t].select(:table_t__curr_id).
  join(:table_c, :curr_id=>:curr_id, :alpha_id=>'XXX').
  join(:table_b, :bic=>:bic, :name=>'Foo')
风流物 2024-10-15 07:37:48

我认为你总是可以使用类似的东西

.filter('table_c.alpha_id = ? AND table_b.name = ?', 'XXX', 'Foo')

I think you can always use something like

.filter('table_c.alpha_id = ? AND table_b.name = ?', 'XXX', 'Foo')
烛影斜 2024-10-15 07:37:48

要记住的一件事是 Sequel 非常乐意让您使用原始 SQL。如果您发现使用 SQL 来表达查询更容易,请务必对其进行注释,以便稍后找到它。然后你可以返回到该行并对其进行调整,以便它能够利用 Sequel 的强大功能。

不过,请尽量避免任何特定于特定 DBM 的内容,因为这会降低可移植性,而这正是使用 ORM 生成查询的重要原因之一。

One thing to remember is that Sequel is plenty happy to let you use raw SQL. If you find it easier to express the query as SQL go ahead, just be sure to comment it so you can find it later. Then you can return to that line and adjust it so it's taking advantage of Sequel's awesomeness.

Try to avoid anything that is specific to a particular DBM though, because you'll be reducing portability which is one of the big reasons for using an ORM to generate the queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文