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

Sql server 如何基于特定列值从多个表中获取数据,sql-server,oracle,Sql Server,Oracle,大家好,我是SQL新手,需要你们的帮助来写下面的查询 我有以下几张桌子 表1 表2 现在,我需要通过在where子句中传递ItemNo来从表1中获取代码,基于此,我将获取代码。然后,我需要获取与该代码关联的所有ItemNo,如果代码仅具有Value=S而不是print以外的其他值,则它是ItemNo和Code。我认为您不需要存在: 你可以试试这个 Select table1.ItemNo, table1.Code, table2.Value from table1 inner join tabl

大家好,我是SQL新手,需要你们的帮助来写下面的查询

我有以下几张桌子

表1

表2

现在,我需要通过在where子句中传递ItemNo来从表1中获取代码,基于此,我将获取代码。然后,我需要获取与该代码关联的所有ItemNo,如果代码仅具有Value=S而不是print以外的其他值,则它是ItemNo和Code。

我认为您不需要存在:

你可以试试这个

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

您可以了解到这一点,因为您是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从表1中获取代码 从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添加第二个条件,如果代码只有值=S 加入表2,在其中添加“S”。 打印项目编号和代码,将代码添加到“选择”

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'
结果: ItemNo代码 12350 B 12351 B
12352 B

sql server还是oracle?你两个都加了标签。但是您可能希望在中使用,或者只使用内部联接就可以用于SQL Server,后者用于oracletoo@John请发布您在编写此查询时所做的任何尝试。提示:table1.ItemNo到table2.ItemNo的内部联接将使您开始使用,并且过滤器将位于Where clauseneverind中,我忘记了您使用notexists。我的错。我太专注于子查询了,哎呀+1.
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')
select t1.ItemNo from #tmpTable1 t1 
where t1.code = (
    select t1.code from #tmpTable1 t1 
    where t1.ItemNo = 12350 )
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'