Related articles with comments I've been made module that shows related articles and based on taxonomy tags. My problem is that, it listed related articles where is no comments too. 

So I want that shows only these articles where is comments.. At least one comment, but I can't get that working. Thank you

My code:

function related_articles($cat, $limit = 10) {

 //$nid = $variables['node']-&gt;nid;
 //$variables['num_comments'] = db_result(db_query('SELECT COUNT(cid) AS count FROM {comments} WHERE nid = %d', $nid));

$limit = isset($limit)?  " LIMIT $limit " : ' ';
$row = db_query('
    SELECT nid \'id\',title \'name\', field_category_tid
    FROM {node} n
    LEFT JOIN {field_data_field_category} fdft ON (fdft.entity_id = n.nid AND bundle = :article AND entity_type = :node)
    WHERE type = :article AND status = :status AND fdft.field_category_tid = :cat order by title asc
    '.$limit, array(':cat' =&gt; $cat, ':article' =&gt; 'article', ':status' =&gt; NODE_PUBLISHED, ':node' =&gt; 'node'))-&gt;fetchAll(PDO::FETCH_OBJ);

return ($row)? $row: array();
}
You will need to join in the comment table and check the comment count.

...
LEFT JOIN {comment} c ON (c.nid = n.nid)
...
HAVING COUNT(c.cid) &gt; 0


The whole query would probably become something like this:

$row = db_query('
  SELECT nid \'id\',title \'name\', field_category_tid
  FROM {node} n
  LEFT JOIN {field_data_field_category} fdft ON (fdft.entity_id = n.nid AND bundle = :article AND entity_type = :node)
  LEFT JOIN {comment} c ON (c.nid = n.nid)
  WHERE type = :article AND status = :status AND fdft.field_category_tid = :cat
  HAVING COUNT(c.cid) &gt; 0
  ORDER BY title asc
  '.$limit, array(':cat' =&gt; $cat, ':article' =&gt; 'article', ':status' =&gt; NODE_PUBLISHED, ':node' =&gt; 'node'))-&gt;fetchAll(PDO::FETCH_OBJ);


Final note, what you want can be easily achieved with the Views module without having to write a query.

UPDATE: 

Count should be checked in HAVING.