假设您有一个架构,在该架构中,将员工与表中的thing1相关联,然后thing1与另一个表中的thing2相关联。我想要一个查询,该查询返回与员工关联的thing2s计数(由于与关联到员工的thing1相关联)。

表格:

employee_thing1(包含employeeID和thing1ID)

something1_thing2(包含thing1ID和thing2ID)

这是我的目的:

 SELECT thing1ID as 'thing1Result' from employee_thing1 where employeeID=?,
 (SELECT 'total' as Name, COUNT(id) from thing1_thing2 as Count where thing1ID=thing1Result);

最佳答案

也许只是使用子查询来获取“ thing1Result”?

SELECT 'total' as Name, COUNT(thing2ID) as Count from thing1_thing2 where thing1ID
in (SELECT thing1ID  from employee_thing1 where employeeID=?)

07-24 19:16