0

我有一个查询如下:

$sult = mysql_query("select * from stories where `categ` = 'businessnews' and `stryid`='".mysql_query("SELECT * FROM comments WHERE `comto`='".mysql_query("select * from stories where `categ` ='businessnews'")." ORDER BY COUNT(comto) DESC")."'  LIMIT 3") or die(mysql_error());
                while($ow=mysql_fetch_array($sult)){

上面的代码应该返回评论最多的前 3 个“故事”{count(comto)}。评论存储在与故事不同的表中。上面的代码不返回任何值,也不显示任何错误。有人可以帮忙吗?

4

2 回答 2

0

您正在将查询传递给 MySQL 服务器,所以让它完成它的工作。

SELECT
   *,
   (SELECT COUNT(1) FROM comments WHERE `comto` = WHERE story_id = s.id) as commentsCount
FROM stories s
WHERE
   s.categ = 'businessnews'
ORDER BY commentsCount DESC
LIMIT 3
于 2013-10-22T15:50:53.853 回答
-1

为这项工作使用子查询... http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

于 2013-10-22T15:47:40.407 回答