需要 SQL 和 Sequel 方面的帮助,涉及内部联接和 where/filter
需要帮助将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是纯粹的 Sequel 方式:
请注意,您也可以在没有 WHERE 的情况下执行此操作,因为您使用的是 INNER JOIN:
This is the pure Sequel way:
Note that you can also do this without a WHERE, since you are using INNER JOIN:
我认为你总是可以使用类似的东西
I think you can always use something like
要记住的一件事是 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.