0

我有两个表我想在 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;
4

3 回答 3

0

@Aurimas Lič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;
于 2013-06-18T11:45:15.563 回答
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');
于 2013-06-18T08:40:46.367 回答
0

如果需要选择右表中不存在的记录,则需要左连接并选择where 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();
于 2013-06-18T08:44:25.350 回答