Where子句和内连接语法查询

时间:2014-09-09 08:56:02

标签: sql sql-server

select SUM(pass_count) ,SUM(fail_count),SUM(blocked_count),SUM(no_run_count)
from TAble1(nolock) table1
where owasp_id = (1,2,3,4,5,8)
inner join 
Table 2 table2
on
TAble1.build_detail_id = table2.build_detail_id 
where 
 TAble1.build_detail_id in(
select top 6 bdt.build_detail_id from table3 bdt order by 1 desc)
and table1.test_run_id=1

我收到语法错误,我希望获得passcount的总和,失败计数,其中owasp_id = 1,2,3,4,5,8,并使用表2进行内部联接以获取构建详细信息ID。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

有三种语法错误,前两种会破坏查询,第三种是强烈建议:

  1. WHERE应该在FROM和任何JOIN条款之后。
  2. 当您提供多个值时,
  3. owasp_id = (1,2,3,4,5,8)应为owasp_id IN (1,2,3,4,5,8)
  4. 不推荐使用WITH表格提示(NOLOCK),应该避免使用。
  5. 因此查询应该如下所示:

    select SUM(pass_count) ,SUM(fail_count),SUM(blocked_count),SUM(no_run_count)
    from Table1 WITH (nolock) table1
    inner join Table2 table2 on TAble1.build_detail_id = table2.build_detail_id 
    where owasp_id IN (1,2,3,4,5,8)
    AND Table1.build_detail_id in(
        select top 6 bdt.build_detail_id from table3 bdt order by 1 desc)
    and table1.test_run_id=1
    

    查询是否按预期工作我不能说,但至少在语法上应该是正确的。