MySQL查询没有选择不同

时间:2016-02-15 20:35:53

标签: php mysqli

我有一张这样的表:

Test_ID   Question_ID    User_ID
---------------------------------
 15           1            1
 15           2            1 
 16           1            2 

我试图通过表格为每个用户打印出类似的内容。 :

测试ID:# 问题数量:#

我正在使用它,我只能打印出测试ID。

 $result = mysqli_query($conn, "SELECT DISTINCT(Test_ID) AS Tcode, COUNT(Question_ID) AS Qcount,User_ID FROM user_answers WHERE User_ID = 1");

   while ($data = mysqli_fetch_array($result)) {

      echo ''.$data['Tcode'].'';
      echo ''.$data['Qcount'].'';


    }

1 个答案:

答案 0 :(得分:4)

将group by用于聚合函数(为此你不需要明确)

"SELECT Test_ID  AS Tcode, COUNT(*) AS Qcount, User_ID
      FROM user_answers
      WHERE User_ID = 1  group by Tcode, User_ID ")