a.operation_name in (select DISTINCT operation_name from mr_operation as c where c.operation_name like CONCAT('%',@operation_name,'%'))这句代码是什么意思
时间: 2024-04-28 15:25:41 浏览: 125
这段代码是一个SQL查询语句,其中涉及到了子查询和模糊匹配。
具体解释如下:
- operation_name是一个列名,表示某个操作的名称。
- @operation_name是一个变量,表示要匹配的操作名称。
- CONCAT()函数将%作为通配符,将@operation_name变量拼接成一个字符串,用于模糊匹配。
- 子查询SELECT DISTINCT operation_name FROM mr_operation as c WHERE c.operation_name LIKE CONCAT('%',@operation_name,'%')用于查询与@operation_name变量相似的操作名称列表,注意这里使用了LIKE运算符进行模糊匹配。
- 最外层查询用IN运算符将操作名称与子查询中查询到的操作名称列表进行匹配,如果匹配成功,则返回结果。
简单来说,这段代码的作用是查询操作名称与@operation_name相似的操作。
相关问题
select distinct a.EMPI_ID, a.PATIENT_NO, a.MR_NO, a.PAT_NAME, a.PAT_SEX, a.PAT_AGE, a.PAT_PHONE_NO, b.DIAG_RESULT, a.ADMIT_DATE, a.DISCHARGE_DEPT_NAME, a.ATTEND_DR from BASIC_INFORMATION a join PA_DIAG b on a.MZZY_SERIES_NO=b.MZZY_SERIES_NO join EXAM_DESC_RESULT_CODE c on a.MZZY_SERIES_NO=c.MZZY_SERIES_NO join DRUG_INFO d on a.MZZY_SERIES_NO=d.MZZY_SERIES_NO join EMR_CONTENT e on a.MZZY_SERIES_NO=e.MZZY_SERIES_NO JOIN TEST_INFO A17 ON a.MZZY_SERIES_NO = A17.MZZY_SERIES_NO where a.PAT_AGE>='18' and (to_char(a.ADMIT_DATE,'YYYY-MM-DD') >= '2021-01-01') AND (b.DIAG_RESULT LIKE '%鼻咽癌%' or b.DIAG_RESULT LIKE '%鼻咽恶性肿瘤%' or b.DIAG_CODE LIKE '%C11/900%') and d.DRUG_NAME not in (select DRUG_NAME FROM DRUG_INFO WHERE DRUG_NAME like '卡培他滨') and b.DIAG_RESULT NOT IN (SELECT DIAG_RESULT FROM PA_DIAG WHERE DIAG_RESULT LIKE '%HIV阳性%') and b.DIAG_RESULT NOT IN (SELECT DIAG_RESULT FROM PA_DIAG WHERE DIAG_RESULT LIKE '%充血性心力衰竭%') AND to_char(( A17.TEST_DETAIL_ITEM_NAME = '中性粒细胞' AND A17.TEST_RESULT >= 1.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '血小板' AND A17.TEST_RESULT >= 100 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '血红蛋白' AND A17.TEST_RESULT >= 9 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '丙氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '天门冬氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '肌酐清除率' AND A17.TEST_RESULT > 51 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '肌酐' AND A17.TEST_RESULT <=1.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '凝血酶原时间' AND A17.TEST_RESULT <= 1.5 ))语句哪里有问题
这个 SQL 语句中有一个问题,就是在最后一个 AND 子句中的 to_char() 函数的参数不正确。to_char() 函数需要一个日期或数字类型的参数,但是这里传入的是一个逻辑类型的表达式。
如果你想要在 WHERE 子句中使用逻辑表达式,则可以使用 CASE WHEN 语句来实现。下面是修改后的 SQL 语句:
```
SELECT DISTINCT
a.EMPI_ID,
a.PATIENT_NO,
a.MR_NO,
a.PAT_NAME,
a.PAT_SEX,
a.PAT_AGE,
a.PAT_PHONE_NO,
b.DIAG_RESULT,
a.ADMIT_DATE,
a.DISCHARGE_DEPT_NAME,
a.ATTEND_DR
FROM
BASIC_INFORMATION a
JOIN PA_DIAG b ON a.MZZY_SERIES_NO = b.MZZY_SERIES_NO
JOIN EXAM_DESC_RESULT_CODE c ON a.MZZY_SERIES_NO = c.MZZY_SERIES_NO
JOIN DRUG_INFO d ON a.MZZY_SERIES_NO = d.MZZY_SERIES_NO
JOIN EMR_CONTENT e ON a.MZZY_SERIES_NO = e.MZZY_SERIES_NO
JOIN TEST_INFO A17 ON a.MZZY_SERIES_NO = A17.MZZY_SERIES_NO
WHERE
a.PAT_AGE >= '18'
AND (TO_CHAR(a.ADMIT_DATE, 'YYYY-MM-DD') >= '2021-01-01')
AND (
b.DIAG_RESULT LIKE '%鼻咽癌%'
OR b.DIAG_RESULT LIKE '%鼻咽恶性肿瘤%'
OR b.DIAG_CODE LIKE '%C11/900%'
)
AND d.DRUG_NAME NOT IN (
SELECT DRUG_NAME
FROM DRUG_INFO
WHERE DRUG_NAME LIKE '卡培他滨'
)
AND b.DIAG_RESULT NOT IN (
SELECT DIAG_RESULT
FROM PA_DIAG
WHERE DIAG_RESULT LIKE '%HIV阳性%'
)
AND b.DIAG_RESULT NOT IN (
SELECT DIAG_RESULT
FROM PA_DIAG
WHERE DIAG_RESULT LIKE '%充血性心力衰竭%'
)
AND (
CASE
WHEN A17.TEST_DETAIL_ITEM_NAME = '中性粒细胞' AND A17.TEST_RESULT >= 1.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '血小板' AND A17.TEST_RESULT >= 100 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '血红蛋白' AND A17.TEST_RESULT >= 9 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '丙氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '天门冬氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '肌酐清除率' AND A17.TEST_RESULT > 51 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '肌酐' AND A17.TEST_RESULT <= 1.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '凝血酶原时间' AND A17.TEST_RESULT <= 1.5 THEN 'Y'
ELSE 'N'
END) = 'Y';
```
在最后一个 AND 子句中,使用了 CASE WHEN 语句来判断测试结果是否符合条件。如果测试结果符合条件,则返回 Y,否则返回 N。然后再判断返回值是否等于 Y。
SELECT DISTINCT A.ID, A.MT_NO, A.OPERATION, A.UNIT, F.STD_TIMES, A.WORK_TIMES, A.STD_CODE, A.UPDATE_BY, A.UPDATE_TIME, F.OPERATION_DESC, F.OPERATION_ENG, F.OPERATION_ACTION, F.CYCLE, F.PART_NO FROM tpmdata.EMS_M_DOC_OPERATION A LEFT JOIN TPMDATA.EMS_M_DOC_HEAD B ON A.MT_NO = B.MT_NO LEFT JOIN tpmdata.EMS_M_SYS_SCHEDULE C ON B.schedule_id = C.ID LEFT JOIN tpmdata.EMS_M_SYS_PLAN D ON D.ID = C.plan_id LEFT JOIN tpmdata.EMS_M_SYS_HEAD E ON D.operation_id = E.ID LEFT JOIN TPMDATA.EMS_M_SYS_ITEM F ON F.OPERATION_GROUP_ID = E.ID where A.MT_NO = '23-2023maintenancePlan-DY-002'; SQL中DISTINCT失效,查询数据有重复,如何用字段中的UPDAT_TIME在查询出来的数据过滤出最新时间的数据,展示SQL
您可以使用子查询和MAX函数来过滤出最新时间的数据,并展示在SQL查询结果中。以下是修改后的SQL查询语句:
```sql
SELECT DISTINCT
A.ID,
A.MT_NO,
A.OPERATION,
A.UNIT,
F.STD_TIMES,
A.WORK_TIMES,
A.STD_CODE,
A.UPDATE_BY,
A.UPDATE_TIME,
F.OPERATION_DESC,
F.OPERATION_ENG,
F.OPERATION_ACTION,
F.CYCLE,
F.PART_NO
FROM
tpmdata.EMS_M_DOC_OPERATION A
LEFT JOIN TPMDATA.EMS_M_DOC_HEAD B ON A.MT_NO = B.MT_NO
LEFT JOIN tpmdata.EMS_M_SYS_SCHEDULE C ON B.schedule_id = C.ID
LEFT JOIN tpmdata.EMS_M_SYS_PLAN D ON D.ID = C.plan_id
LEFT JOIN tpmdata.EMS_M_SYS_HEAD E ON D.operation_id = E.ID
LEFT JOIN TPMDATA.EMS_M_SYS_ITEM F ON F.OPERATION_GROUP_ID = E.ID
WHERE
A.MT_NO = '23-2023maintenancePlan-DY-002'
AND
A.UPDATE_TIME = (
SELECT MAX(UPDATE_TIME)
FROM tpmdata.EMS_M_DOC_OPERATION
WHERE MT_NO = '23-2023maintenancePlan-DY-002'
);
```
这样修改后的查询语句将根据字段中的UPDATE_TIME过滤出最新时间的数据,并确保结果中没有重复的记录。请注意将'MT_NO'值替换为您要过滤的特定值。
阅读全文