使用表1的外键查询对应外键关联表数据
程序员文章站
2022-04-21 10:23:02
...
两个相关联表
TblRecords
的判定规则
字段与TblRule
的id
字段关联
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]
已知TblRecords
的id
时查询对应的规则语句
select * from TblRule where id in (select T1.[判定规则] from TblRecords as T1 where id=ID值)
推荐阅读
-
Mysql数据库中数据表的优化、外键与三范式用法实例分析
-
基于SQL2005 SQL2008 表结构信息查询升级版的详解(含外键信息)
-
MySQL删除有外键约束的表数据方法介绍
-
牛客SQL练习-46-在audit表上创建外键约束,其emp_no对应employees_test表的主键id
-
SQL实战46.在audit表上创建外键约束,其emp_no对应employees_test表的主键id
-
数据库的主表,从表,主键,外键等之间的关系
-
oracle查询所有用户表的表名、主键名称、索引、外键、修改密码等操作详解
-
Postgresql 查询表引用或被引用的外键操作
-
Mysql数据库中数据表的优化、外键与三范式用法实例分析
-
mysql-请问数据库表中可以在一个表里面有两个关联同一个表的外键吗?