两个工作查询的Jooq组合失败,出现FROM子句错误

时间:2016-08-26 18:41:31

标签: sql postgresql jooq

我有两个单独工作的查询,但是当我尝试将它们组合起来时,我收到一条错误消息:“错误:缺少FROM-clause条目表”A“。查询的目的是查看组合两个表的结果,其格式为[“tag1”,“tag2”,“tag3”]的JSONB列,列名为TAGS。单独两个查询都有效,但当我尝试将它们组合起来时,我得到错误。我必须丢失但是我尝试了几种方法无济于事。任何想法如何解决这个问题?结果应该是一个与查询匹配的标签列表,最后是通配符。下面是查询及其组合。我正在使用PostgreSQL 9.5.3

    final Field<String> value = field(name("A", "value"), String.class);

    final Table<Record1<String>> c1 = sql.dsl()
            .selectDistinct(value)
            .from(CAMPAIGN,lateral(table("jsonb_array_elements_text({0})", CAMPAIGN.TAGS)).as("A"))
            .where(CAMPAIGN.STORE_KEY.equal(campaign.getStoreKey()))
            .and(CAMPAIGN.CAMPAIGN_KEY.notEqual(campaignKey))
            .and(value.like(search + "%%"))
            .asTable("c1");

    final Table<Record1<String>> c2 = sql.dsl()
            .selectDistinct(value)
            .from(STOREFRONT, lateral(table("jsonb_array_elements_text({0})", STOREFRONT.TAGS)).as("A"))
            .where(STOREFRONT.STORE_KEY.equal(campaign.getStoreKey()))
            .and(value.like(search + "%%"))
            .asTable("c2");

    final Result<Record1<String>> result = sql.dsl()
            .selectDistinct(value)
            .from(c1, c2)
            .limit(30)
            .fetch();

1 个答案:

答案 0 :(得分:1)

您需要使用Union。试试这个(没有一个带有jooq的工作区来测试,但这样的东西应该有用):

final Field<String> value = field(name("A", "value"), String.class);

final Select<Record1<String>> c1 = sql.dsl()
        .selectDistinct(value)
        .from(CAMPAIGN,lateral(table("jsonb_array_elements_text({0})", CAMPAIGN.TAGS)).as("A"))
        .where(CAMPAIGN.STORE_KEY.equal(campaign.getStoreKey()))
        .and(CAMPAIGN.CAMPAIGN_KEY.notEqual(campaignKey))
        .and(value.like(search + "%%"));

final Select<Record1<String>> c2 = sql.dsl()
        .selectDistinct(value)
        .from(STOREFRONT, lateral(table("jsonb_array_elements_text({0})", STOREFRONT.TAGS)).as("A"))
        .where(STOREFRONT.STORE_KEY.equal(campaign.getStoreKey()))
        .and(value.like(search + "%%"));

final Result<Record1<String>> result = sql.dsl()
        .selectFrom(c1.asTable())
        .union(c2)
        .limit(30)
        .fetch();