Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composite key, in comparison

Tags:

java

sql

oracle

hql

I have three fields that form a unique composite key on a table.

I want to pass in 3 different arrays, where the index matches.

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

is there one sql statement that will return all three rows (assuming they exists), just combining via in won't work to due to "false positives" :

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

database oracle, but via hibernate hql, so ansi preferred if possible ?

like image 370
NimChimpsky Avatar asked Nov 01 '22 04:11

NimChimpsky


2 Answers

OT: your SQL query is probably wrong. It should be:

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

I'm not use whether Hibernate can generate such a query. But in is just a syntax sugar for conjuctions and disjunctions.

like image 164
ibre5041 Avatar answered Nov 13 '22 08:11

ibre5041


You could compose your arrays into a single one, after that:

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);
like image 38
PeterRing Avatar answered Nov 13 '22 06:11

PeterRing