Sample Header Ad - 728x90

SQL to handle multiple AND conditions

3 votes
2 answers
1183 views
I have the following data: |Institution_no | Cred_type | |:-------------- |:----------:| | 1 | 1 | | 1 | 2 | | 2 | 1 | I have a list box so the user can select multiple Cred_type, they want to be able to select either OR or AND conditions. For the OR I have like this AND CRED_TYPE IN (1,2) For the AND, I am really scratching my head. What they are saying is they want a list of institutions that have cred type 1 and cred_type 2. Perhaps I'm not thinking clearly but this is row by row, so doing this would lead to no results. AND cred_type = 1 AND cred_type = 2 -- you can't have a single row have two different values, this would return no results. They require that the user can select 10, 20, or more, so writing out a bunch of code for each and combining them all would be really tough - but this is the only thought I had so far. It would be like this Select institution_no from table where cred_type = 1 UNION Select institution_no from table where cred_type = 2 -- this would combine both and get me what I want, but you could imagine all the code for 10 or 20 of these.
Asked by Rob (133 rep)
Feb 2, 2022, 08:40 PM
Last activity: Feb 3, 2022, 11:52 AM