dsj2014 2017-05-03 02:56
浏览 40
已采纳

php - 从不同的表中选择数据,匹配并计算具有相同值的数字

Edit: I have 2 tables in my database: files and user_logs. I want to print all the values ​​in files and match them with similar values in user_logs.

The columns in user_logs and files are the same:

  +++++++++++++++++++++
  + fileName | fileId +
  +++++++++++++++++++++

And I want the following output:

  +++++++++++++++++++++++++++++++++
  + fileName | no. of same values +
  +++++++++++++++++++++++++++++++++
  + fileNo1 |          3          +
  +++++++++++++++++++++++++++++++++
  + fileNo2 |          1          +
  +++++++++++++++++++++++++++++++++
  + fileNo3 |          0          +
  +++++++++++++++++++++++++++++++++

I don't understand what I researched. Here's my code:

$sql = "SELECT user_logs.fileName,count(*) as c FROM user_logs JOIN files ON user_logs.fileId = files.fileId UNION ALL SELECT files.fileName as file from files a group by a.fileId DESC ";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($result)) {
        $num_row = $row['c'];
    echo "<tr>";
        echo "<td>".$row['fileName']."</td>";
        echo "<td><a href='#' >".$num_row ." / 2 Schools Clicked</a></td>";      
    echo "</tr>";
    }
  • 写回答

1条回答 默认 最新

  • douxian1895 2017-05-03 03:23
    关注

    If I understand you, I think you want something like this as your query:

    SELECT fileName, IF(t.cnt IS NULL, 0, t.cnt) AS c
    FROM files LEFT JOIN
    (SELECT count(*) AS cnt, fileId FROM user_logs GROUP BY fileId) AS t
    ON files.fileId = t.fileId
    ORDER BY fileName
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用