Php 如何在mysql中按另一个表的行数排序
我有这两个表,我想按第二个表的行数排序……我想要一个查询来显示postid和count()每个帖子的评论…我上面的查询工作得很好,但我想添加查询和表posts只是我需要posts表为另一个作业选择*…但它必须在同一个查询上。。。我的问题是,当我将posts表添加到上述查询中时,它将count()乘以8,即posts表中有多少字段。。。它可以做他的工作,像这样,但我不知道这样是否正确,或者可能有其他更简单或正确的方法!提前谢谢Php 如何在mysql中按另一个表的行数排序,php,mysql,sql,Php,Mysql,Sql,我有这两个表,我想按第二个表的行数排序……我想要一个查询来显示postid和count()每个帖子的评论…我上面的查询工作得很好,但我想添加查询和表posts只是我需要posts表为另一个作业选择*…但它必须在同一个查询上。。。我的问题是,当我将posts表添加到上述查询中时,它将count()乘以8,即posts表中有多少字段。。。它可以做他的工作,像这样,但我不知道这样是否正确,或者可能有其他更简单或正确的方法!提前谢谢 comments
comments posts
+---------+------------+ +---------+-----------------+
| Name | Type | | Name | Type |
+---------+------------+ +---------+-----------------+
| id |int(10) prim| +----| postid | int(10) prim |
| name |varchar(128)| | | title | varchar(100) |
| email |varchar(255)| | | image | varchar(150) |
| body | text | | | video | varchar(200) |
| postid | int(10) |--+ | body | text |
| dt | timestamp | | author | varchar(50) |
+---------+------------+ | postdate| timestamp |
| category|enum('1','2','3')|
+---------+-----------------+
<?php
$sql = "SELECT comments.postid, count(*) FROM comments GROUP BY comments.postid ORDER BY count(*) DESC LIMIT 3";
$query = mysqli_query($db_conx, $sql);
?>
Output of that query:
+--------+----------+
| postid | count(*) |
+--------+----------+
| 14 | 6 |
| 13 | 4 |
| 15 | 3 |
+--------+----------+
<?php
$sql = "SELECT comments.postid, count(*) FROM posts, comments GROUP BY comments.postid ORDER BY count(*) DESC LIMIT 3";
$query = mysqli_query($db_conx, $sql);
?>
Output of that query:
+--------+----------+
| postid | count(*) |
+--------+----------+
| 14 | 48 |
| 13 | 32 |
| 15 | 24 |
+--------+----------+
评论帖子
+---------+------------+ +---------+-----------------+
|名称|类型|名称|类型|
+---------+------------+ +---------+-----------------+
|id | int(10)prim |+----posted | int(10)prim|
|姓名|瓦查尔(128)| | |头衔|瓦查尔(100)|
|电子邮件| varchar(255)| | | image | varchar(150)|
|正文|正文| | | |视频| varchar(200)|
|postid | int(10)|-+|正文|文本|
|dt | timestamp | | author | varchar(50)|
+---------+------------+| postdate |时间戳|
|类别|枚举('1','2','3')|
+---------+-----------------+
该查询的输出:
+--------+----------+
|posted | count(*)|
+--------+----------+
| 14 | 6 |
| 13 | 4 |
| 15 | 3 |
+--------+----------+
该查询的输出:
+--------+----------+
|posted | count(*)|
+--------+----------+
| 14 | 48 |
| 13 | 32 |
| 15 | 24 |
+--------+----------+
而不是count(*)您是否尝试过count(distinct comments.id)?这可能会解决乘法问题。天哪,这是解决方案…我不相信:P我花了很多时间编码,我错过了pff…太多了!!如果你想给它一个答案,批准它!