使用exists从SQL Server中选择值

时间:2014-10-22 10:34:42

标签: java sql-server hibernate

我正在为医院临床实验室做一个项目。我从数据库中获取数据时遇到了问题。问题如下。

我有两类测试,即单测试和轮廓测试。主测试表包含测试类别。

测试主表(LAB_TEST_SERVICES)

CREATE TABLE LAB_TEST_SERVICES (inttestid bigint identity NOT NULL IDENTITY,
testid varchar(15) NOT NULL, testname varchar(255) NOT NULL, cptdesc varchar(300),
splinstr varchar(300), tstabbr varchar(25), reordertime numeric(15),
tstduration numeric(15), autocancel numeric(15), minbiltime numeric(15),
status int NOT NULL, maxbiltime numeric(15), PRIMARY KEY (inttestid));

测试的类型和类别存储在下表

LAB_SERVICES_TYPE

CREATE TABLE LAB_SERVICES_TYPE (inttypeid bigint identity NOT NULL IDENTITY,
inttestid bigint NOT NULL, testtype varchar(25), visitype int NOT NULL,
appgendr char(2) NOT NULL, PRIMARY KEY (inttypeid));

如果测试属于单一测试类别,我需要将详细信息填入以下表的列,即LAB_SPECIMEN_MAPPING,LAB_PARAMETER_MAPPING和LAB_TEST_LOCATION。

LAB_SPECIMEN_MAPPING

CREATE TABLE LAB_SPECIMEN_MAPPING (inttestspcid bigint identity NOT NULL IDENTITY,
inttestid bigint NOT NULL, intspcid bigint NOT NULL, intcontid bigint NOT NULL,
volcol numeric(15, 3) NOT NULL, volreq numeric(15, 3) NOT NULL,
status int NOT NULL, PRIMARY KEY (inttestspcid));

LAB_PARAMETER_MAPPING

CREATE TABLE LAB_PARAMETER_MAPPING (intparaid bigint identity NOT NULL IDENTITY,
inttestid bigint NOT NULL, paraname varchar(255) NOT NULL,
parseq numeric(15, 3) NOT NULL, resulttype varchar(30), shortname varchar(30),
mand int NOT NULL, derived int NOT NULL, status int NOT NULL,
PRIMARY KEY (intparaid));

LAB_TEST_LOCATION

CREATE TABLE LAB_TEST_LOCATION (intlocid bigint identity NOT NULL IDENTITY,
intlabid bigint NOT NULL, inttestid bigint NOT NULL, status int NOT NULL,
PRIMARY KEY (intlocid));

如果测试属于Profile Test类别,那么我需要将详细信息填入下表的列,即LAB_PROFILE_TEST_LIST。对于每个配置文件测试,我将单个测试映射到它。

LAB_PROFILE_TEST_LIST

CREATE TABLE LAB_PROFILE_TEST_LIST (intproftestid int identity NOT NULL IDENTITY,
intprotestid int NOT NULL, intsintestid int NOT NULL, PRIMARY KEY (intproftestid));

我已经完成了以下代码来查找映射的测试名称,即如果映射了单个测试,那么testid(来自LAB_TEST_SERVICES的inttestid)将被插入到LAB_SPECIMEN_MAPPING,LAB_PARAMETER_MAPPING和LAB_TEST_LOCATION中,或者如果测试是profile测试testid(来自LAB_TEST_SERVICES的inttestid)将插入LAB_PROFILE_TEST_LIST。

映射的测试(单一和配置文件测试)。(我对我的问题的贡献)。

SELECT *
FROM LAB_TEST_SERVICES lts
WHERE EXISTS
(SELECT lsm.inttestid 
    FROM LAB_SPECIMEN_MAPPING lsm
    WHERE lsm.status = 1
    AND lts.inttestid = lsm.inttestid)
AND EXISTS
(SELECT ltl.inttestid 
    FROM LAB_TEST_LOCATION ltl
    WHERE ltl.status = 1
    AND lts.inttestid = ltl.inttestid) 
AND EXISTS 
(SELECT lptr.intprotestid
    FROM LAB_PROFILE_TEST_LIST lptr WHERE lts.inttestid = lptr.intprotestid)    

因此,当我尝试这样做时,不会出现任何测试,因为我知道没有测试同时具有Single和Profile测试的特性。我做了很多事情,但无法找到解决方案。请帮助我找出详细测试,这些测试既映射单一也发布。我使用的是Java,Hibernate和SQL SERVER。提前致谢

0 个答案:

没有答案