嵌套的mysql select语句

时间:2013-10-22 15:45:52

标签: mysql sql

我的查询如下:

$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)){

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

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