如何基于特定的列值从多个表中获取数据

时间:2019-01-16 17:43:44

标签: sql-server oracle

您好,我是SQL的新手,请在查询下面的文字中获得您的帮助

我有下面的表格

表1

code        ItemNo

A           12345
A           12346
A           12347
A           12348
B           12349
B           12350
B           12351
B           12352
C           12343
C           12354
C           12355
C           12356

表2

ItemNo          Value

12345           S
12346           S
12347           I
12348           B
12349           I
12350           S
12351           S
12352           S
12353           S
12354           S
12355           S
12356           S

现在,我需要通过在where子句中传递ItemNo来从table1中获取代码,我将由此获得Code。比我需要获取与该代码关联的所有ItemNo,并且如果代码的Value = S仅是打印它的ItemNo和代码,则不是其他值。

3 个答案:

答案 0 :(得分:1)

我认为您需要not exists

with cte as (
     select t1.code, t1.itemno, t2.value
     from table1 t1 inner join
          table2 t2
          on t1.itemno = t2.itemno
)
select c.*
from cte c
where not exists (select 1 from cte c1 where c1.code = c.code and c1.value <> 'S')

答案 1 :(得分:0)

您可以尝试

Select table1.ItemNo, table1.Code, table2.Value from table1
inner join table2 on table1.ItemNo = table2.ItemNo
where table1.ItemNo = '12345'

您可以学习此SQL Server Join

答案 2 :(得分:0)

SQL的新手意味着您将需要了解表联接和子查询。我会一次迈出一步,希望对您有所帮助。

创建和填充表的SQL:

IF OBJECT_ID('tempdb..#tmpTable1') IS NOT NULL
  /*Then it exists*/
  DROP TABLE #tmpTable1

CREATE TABLE #tmpTable1 (
    code varchar(4) not null,
    ItemNo smallint not null )

insert into #tmpTable1 VALUES('A', 12345)
insert into #tmpTable1 VALUES('A', 12346)
insert into #tmpTable1 VALUES('A', 12347)
insert into #tmpTable1 VALUES('A', 12348)
insert into #tmpTable1 VALUES('B', 12349)
insert into #tmpTable1 VALUES('B', 12350)
insert into #tmpTable1 VALUES('B', 12351)
insert into #tmpTable1 VALUES('B', 12352)
insert into #tmpTable1 VALUES('C', 12353)
insert into #tmpTable1 VALUES('C', 12354)
insert into #tmpTable1 VALUES('C', 12355)
insert into #tmpTable1 VALUES('C', 12356)

IF OBJECT_ID('tempdb..#tmpTable2') IS NOT NULL
  /*Then it exists*/
  DROP TABLE #tmpTable2

CREATE TABLE #tmpTable2 (
    ItemNo smallint not null,
    Value varchar(4) not null )

insert into #tmpTable2 VALUES(12345, 'S')
insert into #tmpTable2 VALUES(12346, 'S')
insert into #tmpTable2 VALUES(12347, 'I')
insert into #tmpTable2 VALUES(12348, 'B')
insert into #tmpTable2 VALUES(12349, 'I')
insert into #tmpTable2 VALUES(12350, 'S')
insert into #tmpTable2 VALUES(12351, 'S')
insert into #tmpTable2 VALUES(12352, 'S')
insert into #tmpTable2 VALUES(12353, 'S')
insert into #tmpTable2 VALUES(12354, 'S')
insert into #tmpTable2 VALUES(12355, 'S')
insert into #tmpTable2 VALUES(12356, 'S')

对于第一个条件,SQL“通过在where子句中传递ItemNo从table1中获取代码”     从#tmpTable1 t1中选择t1.code     其中t1.ItemNo = 12350

结果: 码 B

SQL添加第二个条件,“获取与该代码关联的所有ItemNo” 将原始查询用作子查询。

select t1.ItemNo from #tmpTable1 t1 
where t1.code = (
    select t1.code from #tmpTable1 t1 
    where t1.ItemNo = 12350 )

结果: 编号 12349 12350 12351 12352

SQL添加第二个条件“并且如果代码仅具有Value = S” 加入Table2,在其中添加“ S”。 “打印它的ItemNo和代码”,将“代码”添加到选择中

select t1.ItemNo, t1.code from #tmpTable1 t1 
inner join #tmpTable2 t2 on t2.ItemNo = t1.ItemNo
where t1.code = (
    select t1.code from #tmpTable1 t1 
    where t1.ItemNo = 12350 )
and t2.Value = 'S'

结果: 项目编号 12350乙 12351乙 12352 B