当一对多关系时,在sql server中获取不同的行

时间:2018-03-08 11:43:17

标签: sql-server

我有2个表(BSH_FLD1)和(BSH_IMGVER)。这些表通过名为(BSH_FLDIMG1)的第三个表相关。(BSH_FLD1)中的一个id字段可以与(BSH_IMGVER)中的许多id图像相关。 创建表的脚本是:

     SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BSH_FLD1](
    [IDFIELD] [int] NOT NULL,
    [EC_NOP] [int] NULL,
    [EC_NOI] [int] NULL,
    [EC_NOA] [int] NULL,
    [INTESTIMI] [varchar](20) COLLATE Albanian_CI_AI NULL,
    [KUTIA] [int] NULL,
    [DREJTORIA] [varchar](30) COLLATE Albanian_CI_AI NULL,
    [DOSJA] [int] NULL,
    [VITI] [int] NULL,
    [AFATI] [varchar](20) COLLATE Albanian_CI_AI NULL,
    [FILLIMI] [datetime] NULL,
    [FUNDI] [datetime] NULL,
    [FAQE] [int] NULL,
    [FILMI] [int] NULL,
    [BLIP] [varchar](10) COLLATE Albanian_CI_AI NULL,
    [DEPARTAMENTI] [varchar](150) COLLATE Albanian_CI_AI NULL,
    [LLOJI_I_DOKUMENTIT] [varchar](50) COLLATE Albanian_CI_AI NULL,
    [ADRESIMI] [varchar](30) COLLATE Albanian_CI_AI NULL,
    [KLASIFIKIMI] [varchar](30) COLLATE Albanian_CI_AI NULL,
    [KOMENTE] [varchar](255) COLLATE Albanian_CI_AI NULL,
    [FRAKSION] [int] NULL,
    [OBJEKTI] [varchar](50) COLLATE Albanian_CI_AI NULL,
 CONSTRAINT [P080318150042500] PRIMARY KEY CLUSTERED 
(
    [IDFIELD] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF





SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BSH_FLDIMG1](
    [IDFIELD] [int] NOT NULL,
    [IDIMAGE] [int] NOT NULL,
 CONSTRAINT [P080318150042718] PRIMARY KEY CLUSTERED 
(
    [IDFIELD] ASC,
    [IDIMAGE] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
USE [eCodis_Data]
GO
ALTER TABLE [dbo].[BSH_FLDIMG1]  WITH CHECK ADD  CONSTRAINT [F080318150042828] FOREIGN KEY([IDFIELD])
REFERENCES [dbo].[BSH_FLD1] ([IDFIELD])
GO
ALTER TABLE [dbo].[BSH_FLDIMG1]  WITH CHECK ADD  CONSTRAINT [F080318150042937] FOREIGN KEY([IDIMAGE])
REFERENCES [dbo].[BSH_IMG] ([IDIMAGE])







SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BSH_IMGVER](
    [IDIMAGE] [int] NOT NULL,
    [VERSION] [int] NOT NULL,
    [IDDATASET] [int] NULL,
    [IDVOLUME] [int] NULL,
    [IMAGE] [varchar](250) COLLATE Albanian_CI_AI NULL,
    [CHECKEDOUT] [smallint] NULL,
    [CHECKOUTBY] [varchar](250) COLLATE Albanian_CI_AI NULL,
    [ADDDATE] [datetime] NULL,
    [ADDBY] [varchar](250) COLLATE Albanian_CI_AI NULL,
    [ORGFNAME] [varchar](250) COLLATE Albanian_CI_AI NULL,
    [NROFPAGES] [int] NULL,
 CONSTRAINT [P080318150040953] PRIMARY KEY CLUSTERED 
(
    [IDIMAGE] ASC,
    [VERSION] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
USE [eCodis_Data]
GO
ALTER TABLE [dbo].[BSH_IMGVER]  WITH CHECK ADD  CONSTRAINT [F080318150041062] FOREIGN KEY([IDIMAGE])
REFERENCES [dbo].[BSH_IMG] ([IDIMAGE])
GO
ALTER TABLE [dbo].[BSH_IMGVER]  WITH CHECK ADD  CONSTRAINT [F080318150041171] FOREIGN KEY([IDDATASET])
REFERENCES [dbo].[BSH_DATASET] ([IDDATASET])
GO
ALTER TABLE [dbo].[BSH_IMGVER]  WITH CHECK ADD  CONSTRAINT [F080318150041281] FOREIGN KEY([IDVOLUME])
REFERENCES [dbo].[VOLUME] ([IDVOLUME])

我想采用不同的行但由于关系是一对多,以下查询会给出与id图像相关的所有id字段

Select DISTINCT
      BSH_FLD1.IDFIELD as name,
      'Dosje' as type,
      'arkivuar' as 'ecm:currentLifeCycleState',
      BSH_FLD1.IDFIELD as 'dc:title',
      BSH_FLD1.IDFIELD,
      BSH_FLD1.EC_NOP,
      BSH_FLD1.EC_NOI,
      BSH_FLD1.EC_NOA,
      BSH_FLD1.INTESTIMI,
      BSH_FLD1.KUTIA,
      BSH_FLD1.DREJTORIA,
      BSH_FLD1.DOSJA,
      BSH_FLD1.VITI,
      BSH_FLD1.AFATI,
      CONVERT(VARCHAR(10), BSH_FLD1.FILLIMI,103) as FILLIMI,
      CONVERT(VARCHAR(10),BSH_FLD1.FUNDI,103) as FUNDI,
      BSH_FLD1.FAQE,
      BSH_FLD1.FILMI,
      BSH_FLD1.BLIP,
      BSH_FLD1.DEPARTAMENTI,
      BSH_FLD1.LLOJI_I_DOKUMENTIT,
      BSH_FLD1.ADRESIMI,
      BSH_FLD1.KLASIFIKIMI,
      BSH_FLD1.KOMENTE,
      BSH_FLD1.FRAKSION,
      BSH_FLD1.OBJEKTI,
      BSH_IMGVER.IDIMAGE,
      BSH_IMGVER.VERSION,
      BSH_IMGVER.IDDATASET,
      BSH_IMGVER.IDVOLUME,
      BSH_IMGVER.IMAGE as 'file:content',
      BSH_IMGVER.CHECKEDOUT,
      BSH_IMGVER.CHECKOUTBY,
      CONVERT(VARCHAR(10),BSH_IMGVER.ADDDATE,103) as ADDDATE,
      BSH_IMGVER.ADDBY,
      BSH_IMGVER.ORGFNAME,
      BSH_IMGVER.NROFPAGES
From BSH_FLDIMG1 
JOIN BSH_FLD1 ON BSH_FLDIMG1.IDFIELD = BSH_FLD1.IDFIELD
JOIN BSH_IMGVER ON BSH_FLDIMG1.IDIMAGE = BSH_IMGVER.IDIMAGE

这给我带来了例如id field 1-idimage 1和idfield 1-idimage 2 。我只想为一个id字段添加一行。 现在结果集是:

|name|type |.......|idImage|
|1   |Dosje|.......|1
|1   |Dosje|.......|2
|2   |Dosje|.......|3
|2   |Dosje|.......|4
|2   |Dosje|.......|5

我希望得到以下内容:

|name|type |.......|idImage|
|1   |Dosje|.......|1
|2   |Dosje|.......|3

有人能帮帮我吗?

0 个答案:

没有答案