2011-12-25 14 views
2

私はこのような列を持つ2つのテーブルがあります。MySQLのサブクエリ(またはUNION?)

Table1 
    testID username topic information  totalTime 
Table2 
    questionID testID question choices  answers 

を、私は同じtestIDからで表1からpartiular testIDと質問の番号NUMBERでのテストのためのすべての列を選択したいと思います表2、結果のテーブルは次のようでなければならないので:

testID username topic information  totalTime questionCount 

テストID & questionIDは、主キーです。

このクエリはどのように形成できますか?ありがとう。

+1

まだあなた自身で試しましたか? – Bojangles

答えて

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

たぶん私はここに何かが欠けていますが、私たちが参加ストレートフォワードの話されていませんか?

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