简体   繁体   English

sql where运算符和in运算符

[英]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. 因此,这里的表1具有称为值2的列。我将它与表2结合在一起,在指定的条件下,表2也具有称为值2的列。 I then join the result with table 3 based on some other condition. 然后根据其他条件将结果与表3合并。 Then I add where operator and in operator, which will return me some specific value1 values from table 4 based on condition specified. 然后添加where运算符和in运算符,它们将根据指定的条件从表4中返回一些特定的value1值。 Thus some unwanted P.value2 values will be skipped. 因此,一些不需要的P.value2值将被跳过。

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? 现在我的问题是:如果我还有另一个表,例如表5,它具有名为value1,value 2和value 3的列,该如何进一步对P.value2值进行排序? I want to leave only P.value2 values, which correspond only to some criteria. 我只想保留P.value2值,该值仅与某些条件相对应。 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! 根据表5中的值获得所需的P.value 2值。感谢您的帮助!

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: 现在我想使用表5对该表进行进一步排序,其中我:

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. 因此,应在此处保留aaa1和aaaa24,并从最终表中删除aaaa32。

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”))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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