gpt4 book ai didi

sql - 在 SQL 和 Sequel 中需要帮助,涉及内部连接和 where/filter

转载 作者:数据小太阳 更新时间:2023-10-29 07:29:27 25 4
gpt4 key购买 nike

需要帮助将sql传输到sequel:
查询语句:

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

最佳答案

这是纯粹的续集方式:

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')

关于sql - 在 SQL 和 Sequel 中需要帮助,涉及内部连接和 where/filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4500135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com