我有两个表--例如TableA和TableB,其中有一些数据,如本文所定义的--如何基于not条件连接两个表的结果
现在我计划为两个表创建Hibernate实体,并且这些实体彼此不相关。
现在我想要得到基于NOT条件的结果,就像我前面提到的文章中给出的那样。
结果是使用SQL查询:
SELECT id, name, partNumber, Aid, Aname, Apart
FROM TableB AS t
CROSS JOIN (SELECT id AS Aid, name AS Aname, partNumber AS Apart
FROM TableA AS a
WHERE NOT EXISTS (SELECT 1
FROM TableB AS b
WHERE b.partNumber = a.partNumber)) AS c
ORDER BY id
现在如何为这样的场景创建HQL查询或条件查询。我已经阅读了HQL&Criteria查询的Hibernate文档,但我不明白如何将此SQL查询转换为HQL和Criteria查询。你能帮我解决这个问题吗。
更新1:
根据弗拉德给出的答案,我没有得到正确的输出。
下面是我编写的代码:
List<Object[]> list = session.createQuery(
"select a, b " + "from TableB b, TableA a "
+ "where b.partNumber != a.partNumber "
+ "ORDER BY b.id").list();
for (Object[] objects : list) {
for (Object object : objects) {
System.out.println(object);
}
}
我得到以下输出:
A: id=2, name=a2, partNumber=20
B: id=5, name=b1, partNumber=10
A: id=3, name=a3, partNumber=30
B: id=5, name=b1, partNumber=10
A: id=4, name=a4, partNumber=40
B: id=5, name=b1, partNumber=10
A: id=1, name=a1, partNumber=10
B: id=6, name=b2, partNumber=20
A: id=3, name=a3, partNumber=30
B: id=6, name=b2, partNumber=20
A: id=4, name=a4, partNumber=40
B: id=6, name=b2, partNumber=20
A: id=1, name=a1, partNumber=10
B: id=7, name=b3, partNumber=60
A: id=2, name=a2, partNumber=20
B: id=7, name=b3, partNumber=60
A: id=3, name=a3, partNumber=30
B: id=7, name=b3, partNumber=60
A: id=4, name=a4, partNumber=40
B: id=7, name=b3, partNumber=60
A: id=1, name=a1, partNumber=10
B: id=8, name=b4, partNumber=70
A: id=2, name=a2, partNumber=20
B: id=8, name=b4, partNumber=70
A: id=3, name=a3, partNumber=30
B: id=8, name=b4, partNumber=70
A: id=4, name=a4, partNumber=40
B: id=8, name=b4, partNumber=70
从输出中,我获得了ID为1,2,3,4
的TableA
的记录&对于ID为TableB
ID'S=5,6,7,8
。
但所需输出的TableA
的ID应为3&4
,而TableB
的ID应为5、6、7、8
。细节在我的另一篇文章中给出:如何基于not条件连接2个表的结果
Hibernate生成的查询是:
Hibernate:
/* select
a,
b
from
TableB b,
TableA a
where
b.partNumber != a.partNumber
ORDER BY
b.id */
select
tablea1_.id as id1_0_0_,
tableb0_.id as id1_1_1_,
tablea1_.name as name2_0_0_,
tablea1_.partNumber as partNumber3_0_0_,
tableb0_.name as name2_1_1_,
tableb0_.partNumber as partNumber3_1_1_
from
TableB tableb0_ cross
join
TableA tablea1_
where
tableb0_.partNumber<>tablea1_.partNumber
order by
tableb0_.id
更新2:
我现在尝试过的代码:
List<Object[]> list = session.createQuery("select b, a "
+ "from TableB b, TableA a "
+ "where not exists ( "
+ "select 1 "
+ "from TableB b1, TableA a1 "
+ "where "
+ "b1.partNumber = a1.partNumber and "
+ "b1.id = b.id and "
+ "a1.id = a.id "
+ ") "
+ "order by b.id").list();
for (Object[] objects : list) {
for (Object object : objects) {
System.out.println(object);
}
}
Hibernate:
select
tableb0_.id as id1_1_0_,
tablea1_.id as id1_0_1_,
tableb0_.name as name2_1_0_,
tableb0_.partNumber as partNumb3_1_0_,
tablea1_.name as name2_0_1_,
tablea1_.partNumber as partNumb3_0_1_
from
TableB tableb0_ cross
join
TableA tablea1_
where
not (exists (select
1
from
TableB tableb2_ cross
join
TableA tablea3_
where
tableb2_.partNumber=tablea3_.partNumber
and tableb2_.id=tableb0_.id
and tablea3_.id=tablea1_.id))
order by
tableb0_.id
此查询得输出:
B: id=5, name=b1, partNumber=10
A: id=4, name=a4, partNumber=40
B: id=5, name=b1, partNumber=10
A: id=2, name=a2, partNumber=20
B: id=5, name=b1, partNumber=10
A: id=3, name=a3, partNumber=30
B: id=6, name=b2, partNumber=20
A: id=1, name=a1, partNumber=10
B: id=6, name=b2, partNumber=20
A: id=4, name=a4, partNumber=40
B: id=6, name=b2, partNumber=20
A: id=3, name=a3, partNumber=30
B: id=7, name=b3, partNumber=60
A: id=3, name=a3, partNumber=30
B: id=7, name=b3, partNumber=60
A: id=1, name=a1, partNumber=10
B: id=7, name=b3, partNumber=60
A: id=4, name=a4, partNumber=40
B: id=7, name=b3, partNumber=60
A: id=2, name=a2, partNumber=20
B: id=8, name=b4, partNumber=70
A: id=3, name=a3, partNumber=30
B: id=8, name=b4, partNumber=70
A: id=1, name=a1, partNumber=10
B: id=8, name=b4, partNumber=70
A: id=4, name=a4, partNumber=40
B: id=8, name=b4, partNumber=70
A: id=2, name=a2, partNumber=20
您需要使用theta样式的连接:
select b, a
from TableB b, TableA a
where not exists (
select 1
from TableB b1, TableA a1
where
b1.partNumber = a1.partNumber and
b1.id = b.id and
a1.id = a.id
)
order by b.id
也可以使用SQL查询来获取实体:
List result = session.createSQLQuery("SELECT b.*, c.* \n" +
"FROM TableB b AS t\n" +
"CROSS JOIN (SELECT id AS Aid, name AS Aname, partNumber AS Apart\n" +
" FROM TableA AS a\n" +
" WHERE NOT EXISTS (SELECT 1\n" +
" FROM TableB AS b\n" +
" WHERE b.partNumber = a.partNumber)) AS c\n" +
"ORDER BY b.id ")
.addEntity("b", B.class)
.addEntity("a", A.class)
.list();
创建表TableA(id int,名称varchar(20),partNumber varchar(30)); 值为: 创建表TableB(id int,名称varchar(20),零件号varchar(30)); 我尝试了以下的查询,所有的结果都是相同的14个记录,这是不正确的。 查询1: 选择b.ID作为Bid,b.Name作为Bname,b.PartNumber作为BPart,a.ID作为A
我有三张桌子 表A 我需要根据传递的参数将TableA连接到TableB或TableC。ie 我尝试了以下查询 但是,这是给语法错误。有人能帮忙吗? 解决方案:
提前道谢!
问题内容: 我有一个名为@status的变量,该变量在此select语句之前设置: 我只想选择if列,否则我想为shipwith选择null。我该如何完成? 问题答案:
基于“SC”代码,我需要将SRCTable与RefTable-1或RefTable-2连接起来 条件:如果SC为“D”,则SRCTable在KEY=KEY1上与RefTable-1连接以获得值。否则,如果SC为“U”,则SRCTable与键=键2上的RefTable-2连接 这是输入spark数据帧。 预期产出: 注意:输入表将有数百万条记录,因此需要一个优化的解决方案