与两个表连接时的cdbcriteria不起作用

时间:2014-02-01 05:15:49

标签: mysql join yii criteria

我有两个表tbl1(model1-pk-cid)和tbl2(model2-fk-cid)tbl1的cid是传统sql工作的tbl的fk但是使用cdbcriteria它会抛出异常.am在model1中编码它

我的传统查询适用于phpmyadmin

 select fp.*
 from tbl1 fp
  left join tbl2 fs  on fs.cid=fp.cid
   where ( fp.cid in (fs.cid) and fp.kat=3)
     group by fp.cid

但cdb条件会抛出异常(在model1中)

$criteria = new CDbCriteria;


    $criteria->join = 'tbl2 as fs ON fs.cid=t.cid';
    $criteria->condition = 't.cid IN (fs.cid) and kat='.$id;
    $criteria->group = 't.cid';

引发的异常是

 SELECT COUNT(*) FROM (SELECT * FROM `tbl1` `t` tbl2 as fs ON fs.cid=cid WHERE cid IN (fs.cid) and kat=3 GROUP BY cid) 

我不明白为什么是这个

SELECT COUNT(*) FROM (

我的查询是

  (SELECT * FROM `tbl1` `t` tbl2 as fs ON fs.cid=cid WHERE cid IN (fs.cid) and kat=3 GROUP BY cid) 

请让我知道无法找出问题

1 个答案:

答案 0 :(得分:1)

首先,您错过了JOIN关键字:

$criteria->join = 'LEFT JOIN tbl2 as fs ON fs.cid=t.cid';
//                 ^^^^ ^^^^