我有两个像这样的列的表:
Table1
testID username topic information totalTime
Table2
questionID testID question choices answers
我想从Table1中选择具有partiular testID
的测试的所有列,并从Table2中选择具有相同testID
的#number问题,因此得到的表应如下所示:
testID username topic information totalTime questionCount
testID& questionID是主键。
如何构成此查询?感谢。
答案 0 :(得分:2)
你可以这样做:
Select t1.testID, t1.username, t1.topic, t1.information, t1.totalTime,
(select count(questionID) from table2 t2 where t2.testID = t1.testID) as 'questionCount'
from table t1
答案 1 :(得分:1)
也许我在这里遗漏了一些东西,但我们不是在谈论直接加入吗?
select t1.testID, t1.username, t1.topic, t1.information, t1.totalTime
,count(t2.questionID) AS questionCount
from table1 t1
,table2 t2
where t1.testID = t2.testID
and t1.testID = :myInputTestID
group by t1.testID, t1.username, t1.topic, t1.information, t1.totalTime