如何 return 来自相同 table 的值?

How to return values from same table?

我有两个 tables A 和 B。我想 return 来自 A 的所有记录并且只匹配来自 B 的记录。我可以为此使用左连接。但是加入后,我想return记录基于同一个标志table。

Table答:

|列 1 |列2 |
|------|------|
| 123 | 12 |
| 456 | 34 |
| 789 | 56 |

Table乙:

|列 1 |列2 | Col3 | Col4 | Col5 |
|-----|-----|-----|-----|-----|
| 123 | 12 |空 |我 | 1 |
| 456 | 34 |空 |乙 | 1 |
| 111 | 98 |空 |我 | 1 |
| 222 | 99 |空 |乙 | 1 |
| 123 | 12 | AB |空 | 2 |
| 456 | 34 |光盘 |空 | 2 |
| 123 | 12 |英孚 |空 | 2 |
| 111 | 98 |生长激素 |空 | 2 |
| 222 | 99 |伊杰 |空 | 2 |

加入 A 和 B 之后,结果会是这样的:

|列 1 |列2 | Col3 | Col4 | Col5 |
|-----|-----|-----|-----|-----|
| 123 | 12 |空 |我 | 1 |
| 456 | 34 |空 |乙 | 1 |
| 123 | 12 | AB |空 | 2 |
| 456 | 34 |光盘 |空 | 2 |
| 123 | 12 |英孚 |空 | 2 |
| 789 | 56 |空 |空 |空 |

Col5 中的 1 和 2 值表示应该填充 Col4 还是 Col3。 1 个用于 Col4,2 个用于 Col3。

我想 return Col4 中 'I' 的所有记录(但不包括具有 'I' 的记录),它看起来像这样:

| Col1 | Col2 | Col3 |   Col4 | Col5 |
|------|------|------|--------|------|
|  123 |   12 |   AB | (null) |    2 |
|  123 |   12 |   EF | (null) |    2 |

我还想 return 在 col4 中记录 'E'(再次排除具有 'E' 的记录),但在 Col3 中记录除 1 以外的所有值。在本例中为 CD。看起来像这样:

|列 1 |列2 | Col3 | Col4 | Col5 |
|------|------|------|--------|------|
| 456 | 34 | AB | (空) | 2 |
| 456 | 34 |英孚 | (空) | 2 |
| 456 | 34 |生长激素 | (空) | 2 |
| 456 | 34 |伊杰 | (空) | 2 |

有人可以建议如何在 SQL 中处理这个问题吗?

select c.col1, c.col2 

from 
(select a.col1, a.col2, b.col3 from  a inner join table b on a.id = b.id
where "condition" ) c

where c.col1 = "condition"

这是脚本。解释是:

在()里面我写了第一个select。在那里,您将使用连接和条件执行 select。在 select 的末尾我写了 "c" 这是从子 select 生成的 table 的名称。 然后,您将从生成的 table 中 select 一些值,并使用将作用于 table 使用子 select 创建的结果的 where 过滤它们

编辑:我使用了你的问题名称以使其更容易

我的结果:-

;with cte1 As(select a.col1,a.col2 from A a left join B b on a.col1 =b.col2 and a.col2=b.col2 where b.col4 = 'I'),cte2 As(select b.col3,b.col4,b.col5 from from A a left join B b on a.col1 =b.col2 and a.col2=b.col2 where b.col4 <> 'I')

E 的结果:-

select a.col1,a.col2,b.col3,b.col4,b.col5 from cte1 a cross join cte2 b 
;with cte1 As(select a.col1,a.col2 from A a left join B b on a.col1 =b.col2 and a.col2=b.col2 where b.col4 = 'E'),cte2 As(select b.col3,b.col4,b.col5 from from A a left join B b on a.col1 =b.col2 and a.col2=b.col2 where b.col4 <> 'E')
select a.col1,a.col2,b.col3,b.col4,b.col5 from cte1 a cross join cte2 b 

好的,我相信以下两个查询可以达到您想要的结果。您可以通过以下SQL Fiddle.

查看所有示例代码

存在规则:

select A.*
     , B.Col3
     , B.Col4
     , B.Col5
  from TableA A
  JOIN TableB B
    on A.Col1 = B.Col1
   and A.Col2 = B.Col2
   and B.Col5 = 2
 where exists (select 1 from TableB C
                where C.col1 = B.col1 and C.col2 = B.col2
                  and c.col4 = 'I' AND C.col5 = 1)

Results:

| Col1 | Col2 | Col3 |   Col4 | Col5 |
|------|------|------|--------|------|
|  123 |   12 |   AB | (null) |    2 |
|  123 |   12 |   EF | (null) |    2 |

排除规则:

select A.*
     , B.Col3
     , B.Col4
     , B.Col5
  from TableA A
 CROSS JOIN TableB B
 where b.col5 = 2
   and exists (select 1 from TableB C
                where C.col1 = a.col1 and C.col2 = a.col2
                  and c.col4 = 'E' AND C.col5 = 1)
   and b.col3 not in (select col3 from TableB b
                       where b.col1 = a.col1 and b.col2 = a.col2 and b.col5 = 2)

Results:

| Col1 | Col2 | Col3 |   Col4 | Col5 |
|------|------|------|--------|------|
|  456 |   34 |   AB | (null) |    2 |
|  456 |   34 |   EF | (null) |    2 |
|  456 |   34 |   GH | (null) |    2 |
|  456 |   34 |   IJ | (null) |    2 |