复合键,相比之下

时间:2015-05-06 16:05:08

标签: java sql oracle hql

我有三个字段在表格上形成唯一的复合键。

我想传入3个不同的数组,其中索引匹配。

custIds= [0,1,2]
custLetters = [A,B,C]
products = ["Cheese","lemons","Aubergine"]

是否有一个sql语句将返回所有三行(假设它们存在), 只是通过in组合工作,因为"误报" :

select * from mytable 
where custId in (custIds)
 and custLetters in (custLetters)
and product in (products);

数据库oracle,但是通过hibernate hql,如果可能的话,首选ansi?

2 个答案:

答案 0 :(得分:1)

在此之后,您可以将阵列组成一个阵列:

custIds= [0,1,2]
custLetters = [A,B,C]
products = ["Cheese","lemons","Aubergine"]

Key=["0ACheese","1Blemons","2CAubergine"]

select * from mytable 
where custId+custLetters+product in (Key);

答案 1 :(得分:1)

OT:您的SQL查询可能有误。它应该是:

select * from mytable 
where (custId, custLetters, product) 
in ( (0, 'A', 'Cheese'),
 (1, 'B', 'lemons'),
 (2, 'C', 'Aubergine'));

我没有使用Hibernate是否可以生成这样的查询。但是in只是结合和分离的语法糖。