如何在MSSQL中使用多关键字(ANSISQL)

时间:2017-03-30 01:31:57

标签: sql sql-server oracle tsql ansi-sql

我知道如何在Oracle中执行以下示例代码,但是如何在ANSI SQL中执行相同的脚本,请帮我处理这个问题。

PS:此脚本用于多关键字(PNO,CASENO,SCRN,IRBCODE,ITEMCODE,MMHCODE,ROWNO)

例如

SELECT * FROM TEST 
WHERE (PNO,CASENO,SCRN,IRBCODE,ITEMCODE,MMHCODE,ROWNO) 
IN 
( 
    SELECT PNO,CASENO,SCRN,IRBCODE,ITEMCODE,MMHCODE,ROWNO FROM TEST 
    WHERE CASENO > 0 AND TO_CHAR(SCRN,'YYYY') = '2017' 
)

如果我的问题不是很清楚,请告诉我

2 个答案:

答案 0 :(得分:2)

您可以在任何数据库中使用EXISTS

SELECT t.*
FROM TEST t
WHERE EXISTS (SELECT 1
              FROM TEST t2
              WHERE t2.CASENO > 0 AND
                    YEAR(t2.SCRN) = 2017 AND
                    t2.PNO = t.PNO AND
                    t2.CASENO = t.CASE_NO AND
                    t2.SCRN = t.SCRN AND
                    t2.IRBCODE = t.IRBCODE AND
                    t2.ITEMCODE = t.ITEMCODE AND
                    t2.MMHCODE = t.MMHCODE AND
                    t2.ROWNO = t.ROWNO
             );

鉴于您正在测试是否在同一个表中,我认为以下内容做同样的事情:

SELECT t.*
FROM TEST t
WHERE t.CASENO > 0 AND TO_CHAR(t.SCRN, 'YYYY') = '2017';

答案 1 :(得分:1)

@Gordon Linoff给了你非常好的例子,我只是改变一点,希望它可以帮到你

SELECT t.*
FROM irbvisit t
WHERE EXISTS (SELECT 1
              FROM irbvisit t2
              WHERE t2.CASENO > 0 AND
                  convert(varchar(4), SCRN, 126)='2017' and 
                    t2.PNO = t.PNO AND
                    t2.CASENO = t.CASENO AND
                    t2.SCRN = t.SCRN AND
                    t2.IRBCODE = t.IRBCODE AND
                    t2.ITEMCODE = t.ITEMCODE AND
                    t2.MMHCODE = t.MMHCODE AND
                    t2.ROWNO = t.ROWNO