我有两个表,我想在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;
use joinInner
for inner join
$select->from('cwi_company','*')->joinInner('cwi_groupinglinks','cwi_company.id != cwi_groupinglinks.orgId')->where('cwi_company.manage=1')->where('cwi_company.deleteOption=0');
如果需要选择右表中不存在的记录,则需要Left Join并选择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();
@Aurimas likus答案的稍微调整版本
$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;