dongshun5963 2014-08-06 16:09
浏览 67
已采纳

单个查询多个SQL查询

I am working on a project where a user can add comments and also hit any post.

Now I have to display the total number of comments and total number of hits and also show whether the user has already hitted that post or not.

So basically I need to do three sql queries for this action:

  • one counting comments,
  • one for counting hits and
  • one for checking whether the user has hitted the post or not.

I wanted to know that if it's possible to reduce these three sql queries into one and reduce the database load?

Any help is appreciated.

$checkifrated=mysql_query("select id from fk_views where (onid='$postid' and hit='hit' and email='$email')");//counting hits
        $checkiffollowing=mysql_query("select id from fk_views where (onid='$postid' and hit='hit' and email='$email')");
 $hitcheck=mysql_num_rows($checkifrated);//checking if already hited or not

$checkifrated=mysql_query("select id from fk_views where (onid='$postid' and comment !='' and email='$email')");//counting comments
  • 写回答

2条回答 默认 最新

  • doumu4916 2014-08-06 16:33
    关注

    This query returns the number of hits and number of nonempty comments.

    select ifnull(sum(hit='hit'),0) as hits, ifnull(sum(comment !=''),0) as comments 
      from fk_views where onid='$postid' and email='$email'
    

    Based on the queries you provided I dont think you need to query separately if he is hitted the post, just check in you code if number of hits is > 0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大