我有一些SQL想要与ZendFW一起使用,但我无法让它工作,这让我发疯。我通过此查询得到了正确的结果:
SELECT DISTINCT e.festival_id FROM entries AS e, mail_log as m
WHERE e.status = 1
AND e.festival_id
NOT IN (SELECT m.entry_id FROM entries AS e, mail_log as m WHERE m.entry_id = e.festival_id)
帮助将不胜感激。干杯:)
答案 0 :(得分:11)
Goran的回答是最好的答案。但是如果你想要一个zend_db_table样式的查询,这将是另一种选择:
$sql = $table->select()
->setIntegrityCheck(false)
->from('entries', new Zend_Db_Expr('DISTINCT festival'))
->where('status = ?', 1)
->where('festival_id NOT IN (?)',new Zend_Db_Expr("SELECT m.entry_id FROM entries AS e, mail_log as m WHERE m.entry_id = e.festival_id"));
答案 1 :(得分:6)
我不确定Zend_Db_Table是否能够处理子查询。
为什么需要使用Zend_Db_Table?
您可以使用Zend_Db :: query()直接执行查询。
$db = Zend_Db_Table::getDefaultAdapter();
$db->query("SELECT DISTINCT e.festival_id FROM entries AS e, mail_log as m
WHERE e.status = 1
AND e.festival_id
NOT IN (SELECT m.entry_id FROM entries AS e, mail_log as m WHERE m.entry_id = e.festival_id)
");
答案 2 :(得分:4)
就我而言,这有效:
$subselect = $db->select()
->from(array('u1' => 'users'), 'departmentId')
->joinInner(array('d' => 'demand'), 'u1.userId = d.adminId', null)
->where('d.demandId = ?', $demand->getId());
$select2 = $db->select()
->from(array('u' => 'users'))
->where('u.departmentId = ?', $subselect);
结果如下:
SELECT `u`.* FROM `users` AS `u` WHERE u.departmentId = (SELECT `u1`.`departmentId` FROM `users` AS `u1` INNER JOIN `demand` AS `d` ON u1.userId = d.adminId WHERE d.demandId = '1')
答案 3 :(得分:0)
我也试试你sql.This是我的代码。
$select = $db->select()
->from(array('e' => ' entries'), array('festival_id'))
->distinct(true)
->where('e.status =?', 1)
->where('e.festival_id NOT IN (SELECT m.entry_id FROM entries AS e, mail_log as m WHERE m.entry_id = e.festival_id)')
->setIntegrityCheck(false);
我认为这段代码比其他代码更具可读性和简单性。