一个较复杂的三个表联合查询,以及取多个列最大值的例子

一个较复杂的三个表联合查询的例子

主要是格式比较复杂,实际没啥技术含量

自己记录一下

select DetailInf.PV0,TestBoardInf.BoardSerial from tb_board_detail_test_info as DetailInf
inner join tb_board_test_info as TestBoardInf on DetailInf.BoardTestID = TestBoardInf.BoardTestID
inner join tb_test_info as TestInf on TestBoardInf.TestID = TestInf.TestID
where DetailInf.TestType=2 and TestInf.TestBatch= 'legetest_20211213112052'

tb_board_detail_test_info里面是具体的测试信息,外键BoardTestID关联表tb_board_test_info,tb_board_test_info里面的外键TestID关联第三个表tb_test_info,我们需要tb_test_info的TestBatch做筛选

最后查询的内容除了tb_board_detail_test_info里面的数据信息,还要查询对应的TestBatch等信息

 

如果最后只是要查表tb_board_detail_test_info里面的数据信息,只需要子查询就行了

这个逻辑我要记录一下

 

除此之外,tb_board_detail_test_info里有PV0、PV10……到PV100的10个列

我需要取这十个列的最大值

select  ( SELECT    MAX(MaxPV)
          FROM      ( VALUES ( DetailInf.PV0), ( DetailInf.PV10),
                    ( DetailInf.PV100) ) AS UpdateDate ( MaxPV) 
          ) AS MaxPV,TestBoardInf.BoardSerial from tb_board_detail_test_info as DetailInf
 inner join tb_board_test_info as TestBoardInf on DetailInf.BoardTestID = TestBoardInf.BoardTestID
   inner join tb_test_info as TestInf on TestBoardInf.TestID = TestInf.TestID
 where DetailInf.TestType=2 and TestInf.TestBatch= 'legetest_20211213112052'  

使用values子句,将每行数据构造为只有一个字段的表,以后求最大值

posted on 2022-09-09 14:30  J·Marcus  阅读(139)  评论(0编辑  收藏  举报

导航