简体   繁体   中英

Querying for multiple values in multiple columns

How would I search multiple (in this example, say 2) columns for an array of values?

For example, here is my table Documents :

1. DocNumber (varchar)
2. CompanyCode (varchar)
3. Data1 (varchar)
4. Data2 (varchar)
5. Data3 (varchar)

DocNumber and CompanyCode form the composite primary key of this table. Say I have a set of values which I want to search in the database such as:

DocNumber CompanyCode
1001 101
1002 102
1004 103

How would I find these unique combinations in the table with one query?

I could use in :

select *
from Documents
where DocNumber in :docNumbers and CompanyCode in :companyCodes

But that would also return records with DocNumber 1001 and CompanyCode 102 (all combinations of the 2 lists). I want to avoid that.

I am using HANA DB (through a Spring Boot application).

Sample table data:
table data

Expected response:
expected response

Response I get:
current response

The IN predicate also works with tuples. Please find a minimal example:

SELECT * 
FROM 
(
    SELECT 'A1' AS DOCNUMBER, 'A2' AS COMPANYCODE FROM DUMMY
)
WHERE (DOCNUMBER, COMPANYCODE) IN (('A1', 'A2'),('B1', 'B2'))

Further details and examples can be found in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM