drnf09037160 2016-02-15 20:35
浏览 39
已采纳

MySQL查询没有选择不同

I have a table like this :

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

And im trying to go through the table and print out something like this for each user. :

Test id : # Number of questions : #

Im using this which I can only get to print out the test 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条回答 默认 最新

  • du512926 2016-02-15 20:38
    关注

    use group by for aggregate function (and for this you don0t need distinct )

    "SELECT Test_ID  AS Tcode, COUNT(*) AS Qcount, User_ID
          FROM user_answers
          WHERE User_ID = 1  group by Tcode, User_ID ")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作