Php SQL SERVER计数和联接

Php SQL SERVER计数和联接,php,sql,sql-server-2008,join,pivot,Php,Sql,Sql Server 2008,Join,Pivot,大家好,我在sql server中进行查询时遇到问题 我有3个表:测试用例、执行和执行错误: 测试用例 处决 执行错误 我需要知道定义了多少测试用例,执行了多少,有多少测试用例有bug,有多少没有bug 我在寻找一个能给出这种结果的查询: testcases_n | executed | with_bugs | without_bugs | bugs_amount --------------------------------------------------------------- 2

大家好,我在sql server中进行查询时遇到问题

我有3个表:测试用例、执行和执行错误:

测试用例

处决

执行错误

我需要知道定义了多少测试用例,执行了多少,有多少测试用例有bug,有多少没有bug

我在寻找一个能给出这种结果的查询:

testcases_n | executed | with_bugs | without_bugs | bugs_amount
---------------------------------------------------------------
2           | 2        | 1         | 1            | 2
考虑到表格结构,这可能吗


谢谢

你的意思是这样的:

declare @testcases as table ( id int, name varchar(16) )
insert into @testcases ( id, name ) values
  ( 1, 'login' ), ( 2, 'Logout' )

declare @executions as table ( id int, testcase_id int )
insert into @executions ( id, testcase_id ) values
  ( 1, 1 ), ( 2, 2 ), ( 3, 1 )

declare @execution_bugs as table ( execution_id int, bug_id varchar(16) )
insert into @execution_bugs ( execution_id, bug_id ) values
  ( 1, 'B-1' ), ( 3, 'B-2' )

select
  ( select count(42) from @testcases ) as testcases_n,
  ( select count(distinct testcase_id) from @executions ) as executed,
  ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as with_bugs,
  ( select count(42) from @testcases ) - ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as without_bugs,
  ( select count(42) from @execution_bugs ) as bugs_amount

我没有考虑使用子查询,因为我需要使用where语句过滤记录。但让我试试这个,我告诉你是怎么回事。非常感谢你!另外,我正在使用PHP和SQL Server—我知道这很奇怪—所以我需要简化查询。嗯,count(42)的作用是什么?count(*)之间有什么区别?@hagensoft-
count(*)
count(42)
返回相同的结果。对于那些坚持使用
选择
而从不使用
*
的人来说,这是一种平静。使用常量表明我考虑了所需的结果,不希望特定列的非NULL值计数。选择
42
仅仅是因为它是。
execution_id | bug_id
----------------------
1            | B-1
3            | B-2
testcases_n | executed | with_bugs | without_bugs | bugs_amount
---------------------------------------------------------------
2           | 2        | 1         | 1            | 2
declare @testcases as table ( id int, name varchar(16) )
insert into @testcases ( id, name ) values
  ( 1, 'login' ), ( 2, 'Logout' )

declare @executions as table ( id int, testcase_id int )
insert into @executions ( id, testcase_id ) values
  ( 1, 1 ), ( 2, 2 ), ( 3, 1 )

declare @execution_bugs as table ( execution_id int, bug_id varchar(16) )
insert into @execution_bugs ( execution_id, bug_id ) values
  ( 1, 'B-1' ), ( 3, 'B-2' )

select
  ( select count(42) from @testcases ) as testcases_n,
  ( select count(distinct testcase_id) from @executions ) as executed,
  ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as with_bugs,
  ( select count(42) from @testcases ) - ( select count(distinct e.testcase_id) from @executions as e inner join @execution_bugs as eb on eb.execution_id = e.id ) as without_bugs,
  ( select count(42) from @execution_bugs ) as bugs_amount