Zend Framework内连接查询中的模型

时间:2013-06-18 08:34:37

标签: php zend-framework

我有两个表我想在zend中编写一个内连接查询来获取表1中所有记录,其id与表2 id不匹配

$db = Zend_Db_Table::getDefaultAdapter();       
$select = $db->select()->setIntegrityCheck(FALSE); 
$select->from('cwi_company','*')->join('cwi_groupinglinks','cwi_company.id <> cwi_groupinglinks.orgId')->where('cwi_company.manage=1')->where('cwi_company.deleteOption=0');
$result =$select->fetchAll();       
return $result;

3 个答案:

答案 0 :(得分:0)

使用joinInner进行内部联接

$select->from('cwi_company','*')->joinInner('cwi_groupinglinks','cwi_company.id != cwi_groupinglinks.orgId')->where('cwi_company.manage=1')->where('cwi_company.deleteOption=0');

答案 1 :(得分:0)

如果你需要选择右表中不存在的记录,你需要左连接并选择value == NULL,例如:

$db = Zend_Db_Table::getDefaultAdapter();       

$select = $db->select()->setIntegrityCheck(FALSE); 
$select->from('cwi_company','*')
  ->joinLeft('cwi_groupinglinks','cwi_company.id = cwi_groupinglinks.orgId')
  ->where('cwi_company.manage=1')
  ->where('cwi_company.deleteOption=0');
  ->where('cwi_groupinglinks.orgId IS NULL')

$result = $select->fetchAll();

答案 2 :(得分:0)

@AurimasLičkus的一点调整版本回答

$db = Zend_Db_Table::getDefaultAdapter();       

$select = $db->select()->setIntegrityCheck(FALSE); 
$select->from('cwi_company',array('*'))
  ->joinLeft('cwi_groupinglinks','cwi_company.id = cwi_groupinglinks.orgId',null)
  ->where('cwi_company.manage = ?',1)
  ->where('cwi_company.deleteOption = ?',0);
  ->where('cwi_groupinglinks.orgId IS NULL')

$result = $select->fetchAll();

return (!empty($result)) ? $result : null;