欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用表1的外键查询对应外键关联表数据

程序员文章站 2022-04-21 10:23:02
...

两个相关联表

TblRecords判定规则字段与TblRuleid字段关联

CREATE TABLE [dbo].[TblRule](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[厚度_min] [float] NOT NULL,
	[厚度_max] [float] NOT NULL,
	[平均厚度_min] [float] NOT NULL,
	[平均厚度_max] [float] NOT NULL,
	[重量_min] [float] NOT NULL,
	[重量_max] [float] NOT NULL,
	[平均重量_min] [float] NOT NULL,
	[平均重量_max] [float] NOT NULL,
	[密度_min] [float] NOT NULL,
	[密度_max] [float] NOT NULL,
	[密度_avg] [float] NOT NULL,
	[检测数量] [int] NULL,
	[UserId] [bigint] NOT NULL,
	[创建日期] [datetime] NULL,
	[修改日期] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[TblRecords](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[UserId] [bigint] NULL,
	[判定规则] [bigint] NULL,
	[检测时间] [datetime] NULL,
	[药片批号] [nvarchar](22) NOT NULL,
	[厚度] [float] NULL,
	[重量] [float] NULL,
	[密度] [float] NULL,
	[温度] [float] NULL,
	[介质密度] [float] NULL,
	[存储报表] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

已知TblRecordsid时查询对应的规则语句

select * from TblRule where id in (select T1.[判定规则] from TblRecords as T1 where id=ID值)