0

我有这个sql:

SELECT link.ID, link.URL, link.ANCHOR, link.HOME, link.CREATED_AT 
FROM `link`, `linkcategory`
WHERE link.ID=linkcategory.LINK_ID
GROUP BY linkcategory.LINK_ID 
HAVING count(linkcategory.category_id) =(select count(*) from categoria)

我正在尝试生成标准,这是不起作用的标准:

$c = new Criteria();
$c->addJoin(self::ID, LinkcategoryPeer::LINK_ID);
$c->addAsColumn('catCount','(SELECT COUNT(*) FROM CATEGORIA)');
$c->addGroupByColumn(LinkcategoryPeer::LINK_ID);
$having = $c->getNewCriterion(count(LinkcategoryPeer::CATEGORY_ID),$c->getColumnForAs('catCount'));
$c->addHaving($having); 
return self::doSelect($c);

这个条件的返回 sql 是这个:

SELECT (SELECT COUNT(*) FROM CATEGORIA) AS catCount 
FROM `link`, `linkcategory` 
WHERE link.ID=linkcategory.LINK_ID 
GROUP BY linkcategory.LINK_ID 
HAVING 1='(SELECT COUNT(*) FROM CATEGORIA)'

我真的不知道为什么条件会错误地转换 sql。有谁知道错误在哪里?

4

1 回答 1

0

试试这个查询。我不记得如何link.*使用 Propel 执行。但我认为问题having在于您使用的是 PHP 计数函数而不是 MySQL 函数。

$c = new Criteria();
$c->addSelectColumn(self::ID);
$c->addSelectColumn(self::URL);
$c->addSelectColumn(self::ANCHOR);
$c->addSelectColumn(self::HOME);
$c->addSelectColumn(self::CREATED_AT );

$c->addJoin(self::ID, LinkcategoryPeer::LINK_ID);
$c->addGroupByColumn(LinkcategoryPeer::LINK_ID);

$having = $c->getNewCriterion(
  'COUNT('.LinkcategoryPeer::CATEGORY_ID.')'),
  $c->getColumnForAs('catCount')
);
$c->addHaving($having);

return self::doSelect($c);
于 2012-09-20T13:45:24.347 回答