我在我的SQL查询中集成NOT EXISTS时遇到问题。让我详细解释一下这个问题 我有四个表:branch_details,transactions,branch_order_relation和branch_pincode_relation。
这是我的SQL查询
private static final String SELECT_ORDERS_BY_BRANCH =
"select transaction_id,source_id,destination_id
from transactions,branch_pincode_relation,branch_details,branch_order_relation
where branch_details.branch_email = ?
and branch_details.branch_id = branch_pincode_relation.branch_id
and branch_pincode_relation.origin_pincode = transactions.start_pin
and transactions.parent_transaction_id IS NOT NULL
and transactions.order_status = "+JiffieConstants.PAYMENT_SUCCESS;
有一些transaction_id of transactions表存在于branch_order_relation中(作为order_id)。因此,如果transaction_id存在于branch_order_relation中,我们就不应该选择它。否则我们需要选择它。任何人都可以在上面的SQL查询中集成它。我也尝试过google但是却无法提出解决方案
答案 0 :(得分:1)
我将查询修改为
private static final String SELECT_ORDERS_BY_BRANCH =" select transaction_id,source_id,destination_id from transactions,branch_pincode_relation,branch_details where branch_details.branch_email =?和branch_details.branch_id = branch_pincode_relation.branch_id和branch_pincode_relation.origin_pincode = transactions.start_pin和transactions.parent_transaction_id IS NOT NULL且transactions.order_status =" + JiffieConstants.PAYMENT_SUCCESS +"和NOT EXISTS(从branch_order_relation中选择null,其中branch_order_relation.order_id = transactions.transaction_id)&#34 ;;
及其工作。谢谢!
答案 1 :(得分:0)
在transactions
和branch_order_relation
上使用左外连接,并添加where branch_order_relation.order_id is null
子句。
这将选择不在branch_order_relation
中的交易,见下文
select transaction_id,source_id,destination_id
from transactions
left outer join branch_order_relation on transactions.transaction_id = branch_order_relation.order_id
where branch_order_relation.order_id is null
然后加入其他表格,并添加你的where子句