如何在 Spring 框架中使用 Select Distinct 和 Left Join 删除重复项
How to Remove duplicates with Select Distinct and Left Join in Spring Framework
我有两个 table,即 FUTURE_DATED_TRANSACTION 和 TRANSACTION_JOURNAL 。我想我有点知道如何过滤记录是否仅存在于一个 table 上,但是我不能完全与左连接(第二个 table)一起完成。
我的 TRANSACTION_JOURNAL table 有一个包含重复值的 FUTURE_DATED_TRANSACTION_ID 字段,我希望它从输出中消失,因为它只是从 parent 输出重复值table 如下例所示。
我现在的输出是这样的
FUTURE_DATED_TRANSACTION_1
- TRANSACTION_JOURNAL_1
- TRANSACTION_JOURNAL_1
- TRANSACTION_JOURNAL_1
我的预期输出是
FUTURE_DATED_TRANSACTION_1
- TRANSACTION_JOURNAL_1
如何让它输出成那样?
我的代码片段是
private final DSLContext dsl;
........
return this.dsl
.select(
FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID,
FUTURE_DATED_TRANSACTION.REFERENCE_NUMBER,
FUTURE_DATED_TRANSACTION.RECURRING_FREQUENCY,
FUTURE_DATED_TRANSACTION.RECURRING_DAY_OF_THE_MONTH,
FUTURE_DATED_TRANSACTION.RECURRING_DAY_OF_THE_WEEK,
FUTURE_DATED_TRANSACTION.TRANSFER_SCHEDULE_DATE,
FUTURE_DATED_TRANSACTION.RECURRING_START_DATE,
FUTURE_DATED_TRANSACTION.RECURRING_END_DATE,
FUTURE_DATED_TRANSACTION.AMOUNT,
FUTURE_DATED_TRANSACTION.STATUS,
FUTURE_DATED_TRANSACTION.STATUS_REMARKS,
FUTURE_DATED_TRANSACTION.CURRENCY_ID,
TRANSACTION_JOURNAL.ID.as("transactions_id"),
TRANSACTION_JOURNAL.REFERENCE_NUMBER.as("transactions_childReferenceNumber"),
TRANSACTION_JOURNAL.SOURCE_ACCOUNT_NUMBER.as("transactions_debtorAccountId"),
TRANSACTION_JOURNAL.SOURCE_ACCOUNT_TYPE.as("transactions_debtorAccountType"),
TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_NUMBER.as("transactions_creditorAccountId"),
TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_TYPE.as("transactions_creditorAccountType"),
TRANSACTION_JOURNAL.CURRENCY_ID.as("transactions_debtorAccountCurrencyId"),
TRANSACTION_JOURNAL.CURRENCY_ID_DESTINATION.as("transactions_creditorAccountCurrencyId"),
TRANSACTION_JOURNAL.AMOUNT.as("transactions_amount"),
TRANSACTION_JOURNAL.SERVICE_FEE.as("transactions_serviceFee"),
TRANSACTION_JOURNAL.TRANSACTION_DATE.as("transactions_transferScheduledDate"),
TRANSACTION_JOURNAL.ESB_POSTED_DATE.as("transactions_postedDate"),
TRANSACTION_JOURNAL.STATUS.as("transactions_transferStatus"),
TRANSACTION_JOURNAL.STATUS_REMARKS.as("transactions_statusRemarks"),
TRANSACTION_JOURNAL.TRANSFER_SCHEDULE_TYPE.as("transactions_transactionType"),
TRANSACTION_JOURNAL.REMARKS.as("transactions_remarks"),
TRANSACTION_JOURNAL.POSTING_DATE.as("transactions_completionDate"),
TRANSACTION_JOURNAL.CREDITOR_DETAILS.as("transactions_creditorDetails"),
SEND_MONEY_TYPE.CODE.as("transactions_sendMoneyCode"),
SEND_MONEY_TYPE.SEND_MONEY_TYPE_NAME.as("transactions_sendMoneyType")
)
.from(
this.dsl
.select(FUTURE_DATED_TRANSACTION.fields())
.from(FUTURE_DATED_TRANSACTION)
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter))
.and(this.addWildCardQueryParent(query))
.and((TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_NUMBER.in(accountList)
.and(TRANSACTION_JOURNAL.STATUS.eq(TransactionStatus.POSTED.toLabel())))
.or(TRANSACTION_JOURNAL.CUSTOMER_NUMBER.eq(userId)))
.groupBy(FUTURE_DATED_TRANSACTION.fields())
.orderBy(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID.desc())
.limit(limit)
.offset(offset - 1)
.asTable("future_dated_transaction")
)
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.leftJoin(SEND_MONEY_TYPE)
.on(TRANSACTION_JOURNAL.SEND_MONEY_TYPE_ID.eq(SEND_MONEY_TYPE.ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter));
我已经尝试了 Select 不同的迭代,如下所示,
但我一直收到
的错误
.....nested exception is java.sql.SQLSyntaxErrorException: You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'on
(transaction_journal
.future_dated_transaction_id
)
`future_dated_transacti' at line 1"
这与我在使用 Select Distinct 的不同代码迭代中遇到的情况几乎相同。我当前的设置真的不支持这种排序还是我的代码有误? TIA.
哦,没关系,在尝试在 select 不同查询之间迭代一整天之后。我放弃并尝试单击所有内容并检查智能感知说的对我可用的内容,在这里我发现 groupBy 是适合我的那个。我非常专注于使用 Select Distinct,因为这是 SQL 命令,我知道它适用于我的用例。显然还有另一种方法,或者很可能是唯一的方法。如果有人可以向我推荐一种使用 Select Distinct Query 执行此操作的方法,我会将其标记为答案。同时,这是我所做的(只需查看上面问题的完整代码片段,我将其截断以减少此处的代码长度):)
..................
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.leftJoin(SEND_MONEY_TYPE)
.on(TRANSACTION_JOURNAL.SEND_MONEY_TYPE_ID.eq(SEND_MONEY_TYPE.ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter))
.groupBy(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID);
我有两个 table,即 FUTURE_DATED_TRANSACTION 和 TRANSACTION_JOURNAL 。我想我有点知道如何过滤记录是否仅存在于一个 table 上,但是我不能完全与左连接(第二个 table)一起完成。
我的 TRANSACTION_JOURNAL table 有一个包含重复值的 FUTURE_DATED_TRANSACTION_ID 字段,我希望它从输出中消失,因为它只是从 parent 输出重复值table 如下例所示。
我现在的输出是这样的
FUTURE_DATED_TRANSACTION_1
- TRANSACTION_JOURNAL_1
- TRANSACTION_JOURNAL_1
- TRANSACTION_JOURNAL_1
我的预期输出是
FUTURE_DATED_TRANSACTION_1
- TRANSACTION_JOURNAL_1
如何让它输出成那样?
我的代码片段是
private final DSLContext dsl;
........
return this.dsl
.select(
FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID,
FUTURE_DATED_TRANSACTION.REFERENCE_NUMBER,
FUTURE_DATED_TRANSACTION.RECURRING_FREQUENCY,
FUTURE_DATED_TRANSACTION.RECURRING_DAY_OF_THE_MONTH,
FUTURE_DATED_TRANSACTION.RECURRING_DAY_OF_THE_WEEK,
FUTURE_DATED_TRANSACTION.TRANSFER_SCHEDULE_DATE,
FUTURE_DATED_TRANSACTION.RECURRING_START_DATE,
FUTURE_DATED_TRANSACTION.RECURRING_END_DATE,
FUTURE_DATED_TRANSACTION.AMOUNT,
FUTURE_DATED_TRANSACTION.STATUS,
FUTURE_DATED_TRANSACTION.STATUS_REMARKS,
FUTURE_DATED_TRANSACTION.CURRENCY_ID,
TRANSACTION_JOURNAL.ID.as("transactions_id"),
TRANSACTION_JOURNAL.REFERENCE_NUMBER.as("transactions_childReferenceNumber"),
TRANSACTION_JOURNAL.SOURCE_ACCOUNT_NUMBER.as("transactions_debtorAccountId"),
TRANSACTION_JOURNAL.SOURCE_ACCOUNT_TYPE.as("transactions_debtorAccountType"),
TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_NUMBER.as("transactions_creditorAccountId"),
TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_TYPE.as("transactions_creditorAccountType"),
TRANSACTION_JOURNAL.CURRENCY_ID.as("transactions_debtorAccountCurrencyId"),
TRANSACTION_JOURNAL.CURRENCY_ID_DESTINATION.as("transactions_creditorAccountCurrencyId"),
TRANSACTION_JOURNAL.AMOUNT.as("transactions_amount"),
TRANSACTION_JOURNAL.SERVICE_FEE.as("transactions_serviceFee"),
TRANSACTION_JOURNAL.TRANSACTION_DATE.as("transactions_transferScheduledDate"),
TRANSACTION_JOURNAL.ESB_POSTED_DATE.as("transactions_postedDate"),
TRANSACTION_JOURNAL.STATUS.as("transactions_transferStatus"),
TRANSACTION_JOURNAL.STATUS_REMARKS.as("transactions_statusRemarks"),
TRANSACTION_JOURNAL.TRANSFER_SCHEDULE_TYPE.as("transactions_transactionType"),
TRANSACTION_JOURNAL.REMARKS.as("transactions_remarks"),
TRANSACTION_JOURNAL.POSTING_DATE.as("transactions_completionDate"),
TRANSACTION_JOURNAL.CREDITOR_DETAILS.as("transactions_creditorDetails"),
SEND_MONEY_TYPE.CODE.as("transactions_sendMoneyCode"),
SEND_MONEY_TYPE.SEND_MONEY_TYPE_NAME.as("transactions_sendMoneyType")
)
.from(
this.dsl
.select(FUTURE_DATED_TRANSACTION.fields())
.from(FUTURE_DATED_TRANSACTION)
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter))
.and(this.addWildCardQueryParent(query))
.and((TRANSACTION_JOURNAL.DESTINATION_ACCOUNT_NUMBER.in(accountList)
.and(TRANSACTION_JOURNAL.STATUS.eq(TransactionStatus.POSTED.toLabel())))
.or(TRANSACTION_JOURNAL.CUSTOMER_NUMBER.eq(userId)))
.groupBy(FUTURE_DATED_TRANSACTION.fields())
.orderBy(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID.desc())
.limit(limit)
.offset(offset - 1)
.asTable("future_dated_transaction")
)
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.leftJoin(SEND_MONEY_TYPE)
.on(TRANSACTION_JOURNAL.SEND_MONEY_TYPE_ID.eq(SEND_MONEY_TYPE.ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter));
我已经尝试了 Select 不同的迭代,如下所示,
但我一直收到
的错误.....nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on (
transaction_journal
.future_dated_transaction_id
) `future_dated_transacti' at line 1"
这与我在使用 Select Distinct 的不同代码迭代中遇到的情况几乎相同。我当前的设置真的不支持这种排序还是我的代码有误? TIA.
哦,没关系,在尝试在 select 不同查询之间迭代一整天之后。我放弃并尝试单击所有内容并检查智能感知说的对我可用的内容,在这里我发现 groupBy 是适合我的那个。我非常专注于使用 Select Distinct,因为这是 SQL 命令,我知道它适用于我的用例。显然还有另一种方法,或者很可能是唯一的方法。如果有人可以向我推荐一种使用 Select Distinct Query 执行此操作的方法,我会将其标记为答案。同时,这是我所做的(只需查看上面问题的完整代码片段,我将其截断以减少此处的代码长度):)
..................
.leftJoin(TRANSACTION_JOURNAL)
.on(FUTURE_DATED_TRANSACTION.FUTURE_DATED_TRANSACTION_ID
.eq(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID))
.leftJoin(SEND_MONEY_TYPE)
.on(TRANSACTION_JOURNAL.SEND_MONEY_TYPE_ID.eq(SEND_MONEY_TYPE.ID))
.where(this.getQueryConditions(sendMoneyScheduledFilter))
.groupBy(TRANSACTION_JOURNAL.FUTURE_DATED_TRANSACTION_ID);