所以我有一个包含消息和thread_id的表(t1)。每个线程ID对应一个对话。我对消息中的术语运行查询,现在我想根据消息查询的结果重新查询t1。我不能做一个嵌套的,因为最终的结果应该来自原来的t1。重申:我想要包含某些单词的所有对话。这是在postgresql 9.1中。
答案 0 :(得分:1)
这适用于Common Table Experssions,可从版本8.4及更高版本获得:
create table test_table1 (id serial, val1 integer, val2 text);
insert into test_table1 (val1, val2)
select
v, 'test data ' || v
from generate_series(1,10000);
WITH
t1 (id, val1, val2)
as (
select
id, val1, val2
from test_table1
order by id asc
limit 1000
)
,t1_condition (id, condition)
as (
select
id, val1 % 5 = 0
from t1
where val1 % 3 = 0
)
select
t1.id, t1.val1, t1.val2,
t1_condition.condition
from t1
left join t1_condition
on t1.id = t1_condition.id