我有以下代码可以正常工作:
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))
所以这里表1有一个名为value 2的列。我将它与表2连接起来,表2在指定的条件下也有一个名为value 2的列。然后我根据其他条件将结果与表3结合起来。然后我添加where运算符和运算符,它将根据指定的条件从表4返回一些特定的value1值。因此,将跳过一些不需要的P.value2值。
现在我的问题是:如果我有另一个表,比如表5,其中包含名为value1,value 2和value 3的列,如何进一步对P.value2值进行排序?我想只留下P.value2值,它们只对应于某些标准。我尝试了这段代码,但它不起作用:
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”))
这是使用操作员的正确方法吗?我该如何优化它?再一次,我想要实现的是添加这一点:
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”))
根据表5中的值获得所需的P.value 2值。感谢您的帮助!
所以我们联合表说:
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
我们有选定的(排序的)表格:
value1 value2
------ ------
eeeee1 aaaaa1
eeee24 aaaa24
eeee32 aaaa32
...etc
现在我想使用表5进一步对该表进行排序,其中i:
value1 value2 value3
------ ------ ------
12223 aaaa12 ccc
12334 aaaa32 bbb
aaaa1 123344 ccc
12332 aaaa24 ccc
因此这里应该留下aaaa1和aaaa24,并且应该从决赛桌中删除aaaa32。
答案 0 :(得分:0)
更改
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”))
要
and P.value2 in (select value1 from table 5
where (P.value2 = value1 OR P.value2 = value2) and (value3 = “ccc”))