存在SQL语句

时间:2013-05-02 12:13:33

标签: sql exists

我正在尝试在表格中找到所有yearcaseseqnumbers,其中类型不是上诉人Rep 1.错误来自于year和{{1表中可以有很多行。这就是我的尝试:

caseseqnumber

任何帮助?!

5 个答案:

答案 0 :(得分:2)

为什么你需要在那里进行嵌套搜索。只有在检查多个数据库表中的内容时才需要嵌套搜索。

坚持

select caseqnumber,year from caseparticipants where paticipanttype <> 'Appellant Rep 1'

(&lt;&gt;是NOT EQUAL TO的sql子句)

答案 1 :(得分:1)

Select 
    caseseqnumber, 
    year 
from 
    caseparticipants 
where 
    participanttype != 'Appellant Rep 1'

答案 2 :(得分:0)

为什么不尝试直接在主查询中使用WHERE条件而不是使用子查询?

SELECT caseseqnumber, year 
  FROM caseparticipants 
 WHERE participanttype != 'Appellant Rep 1'

答案 3 :(得分:0)

您可以使用LEFT JOIN加入caseparticipants。如果联接没有成功,则意味着caseseqnumber和year没有participanttype = 'Appellant Rep 1'行:

SELECT
  c1.caseseqnumber, 
  c1.year 
FROM 
  caseparticipants c1 LEFT JOIN caseparticipants c2
  ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber
     AND c2.participanttype = 'Appellant Rep 1'
WHERE
  c2.year IS NULL

修改

要比较caseseqnumber,year和具有“Appellant Rep 1”类型的组合数量的不同组合的数量,您可以使用此SQL Server查询:

SELECT
  COUNT(DISTINCT
    CAST(c1.caseseqnumber AS VARCHAR) + '-' + CAST(c1.year AS VARCHAR)),
  COUNT(DISTINCT
    CAST(c2.caseseqnumber AS VARCHAR) + '-' + CAST(c2.year AS VARCHAR))
FROM 
  caseparticipants c1 LEFT JOIN caseparticipants c2
  ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber
     AND c2.participanttype = 'Appellant Rep 1'

答案 4 :(得分:0)

如果您希望所有案件都有上诉人但不是代表:

Select caseseqnumber, year
from caseparticipants cp1
where not exists (Select null
                  from caseparticipants cp2
                  where cp2.participanttype = 'Appellant Rep 1'
                  and cp1.caseseqnumber = cp2.caseseqnumber
                  and cp1.year = cp2.year
)