简体   繁体   中英

sql where operator and in operator

I have the following code which works fine:

select 
     M.value1, P.value2 
from 
     table 1 P
join 
     table 2 E on (P.value2 = E.value2)
join 
     table 3 M on ( something else here)
where 
     P.value2 in (select value1 
                  from table 4
                  where (value 2 = “aaa”) and (value 3 = “bbb))

So here table 1 has column called value 2. I join it with table 2, which also has column called value 2 on the condition specified. I then join the result with table 3 based on some other condition. Then I add where operator and in operator, which will return me some specific value1 values from table 4 based on condition specified. Thus some unwanted P.value2 values will be skipped.

Now my question is: if I have another table, say table 5, which has for instance columns called value1, value 2 and value 3 how do I sort P.value2 values further? I want to leave only P.value2 values, which correspond only to some criteria. I tried this code, but it didn't work:

select 
    M.value1, P.value2 
from
    table 1 P 
join 
    table 2 E on (P.value2 = E.value2) 
join 
    table 3 M on ( something else here) 
where 
    P.value2 in (select value1 
                 from table 4 
                 where (value2 = “aaa”) and (value3 = “bbb))
    and P.value2 in (select value1 
                     from table 5 
                     where (P.value2 = value1) and (value3 = “ccc”))  
    or P.value2 in (select value2 
                    from table 5
                    where (P.value2 = value2) and (value3 = “ccc”))

Is this a correct way to use where operator and how could I optimize this? Once again, what I want to achieve is to add this bit:

 and P.value2 in (select value1 from table 5 where (P.value2 = value1) and (value3 = “ccc”)) or P.value2 in (select value2 from table 5 where (P.value2 = value2) and (value3 = “ccc”)) 

to obtain desired P.value 2 values based on values from table 5. Thanks for any help!

So we have the joined table say:

value1 value2 value3 value1 value2 value1 value2
------ ------ ------ ------ ------ ------ ------
bbbbb1 aaaaa1 ccccc1 ddddd1 aaaaa1 eeeee1 fffff1
bbbb24 aaaa24 cccc24 dddd24 aaaa24 eeee24 ffff24
bbbb32 aaaa32 cccc32 dddd32 aaaa32 eeee32 ffff32
bbbb42 aaaa42 cccc42 dddd42 aaaa42 eeee42 ffff42
..... etc

and we have the selected (sorted) table:

value1 value2
------ ------
eeeee1 aaaaa1
eeee24 aaaa24
eeee32 aaaa32
...etc 

and now i want to sort this table further using table 5 which i:

value1 value2 value3
------ ------ ------
12223  aaaa12 ccc
12334  aaaa32 bbb
aaaa1  123344 ccc
12332  aaaa24 ccc

therefore here aaaa1 and aaaa24 should be left and aaaa32 should be removed from the final table.

Change

and P.value2 in (select value1 from table 5
where (P.value2 = value1) and (value3 = “ccc”)) 
or P.value2 in (select value2 from table 5
where (P.value2 = value2) and (value3 = “ccc”))

To

and P.value2 in (select value1 from table 5
where (P.value2 = value1 OR P.value2 = value2) and (value3 = “ccc”))

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