【问题标题】:Zendframework Union IssueZendframework 联盟问题
【发布时间】:2013-09-10 13:47:14
【问题描述】:

我正在运行这段代码

$sq = $this->_codes->getAdapter()->select()
            ->from (array('cs' => 'code_statuses'), array('total' =>     'count(*)'))
            ->join (
                array ('c' => 'codes'), 'c.code_id = cs.code_id', 
                array ('human_state' => new Zend_Db_Expr("CASE c.state_id WHEN 3 THEN 'active' WHEN 5 THEN 'suspended' ELSE 'inactive' END"), 'c.*')
            )
            ->group('cs.code_id');

$sqtemp = $this->_codes->getAdapter()->select()
            ->from (array('cs' => 'code_statuses'), array('total' => 'count(*)'))
            ->join (
                array ('c' => 'codes'), 'c.code_id = cs.code_id', 
                array ('human_state' => new Zend_Db_Expr("CASE     c.state_id WHEN 3 THEN 'active' WHEN 5 THEN 'suspended' ELSE 'inactive' END"), 'c.*')
            )
            ->group('cs.code_id');

if (!empty($options['state_id'])):
            if (is_array($options['state_id'])):
                $states = 'cs.state_id=' . implode(' OR cs.state_id=', $options['state_id']);
                $sq->where($states)
                                       ->having(total<=4);
                $sqtemp->where ('cs.state_id=5')
                                            ->having(total<4);
            else:
                $sq->where ('cs.state_id=?', $options['state_id']);
            endif;

当我尝试使用联合时出现问题

$sqfinal=$this->_codes->getAdapter()->select()
                ->union(array($sq,$sqtemp))
                ->order('cs.code_id');

但单独 $sq$sqtemp 工作正常

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cs.code_id' in 'order clause'

不知道哪里出错了

任何帮助将不胜感激

*编辑

SELECT count(*) AS `total`, 
CASE c.state_id 
WHEN 3 THEN 'active' 
WHEN 5 THEN 'suspended' 
ELSE 'inactive' 
END AS `human_state`, `c`.* 
FROM `code_statuses` AS `cs` 
INNER JOIN `codes` AS `c` 
ON c.code_id = cs.code_id 
WHERE (cs.state_id=1 OR cs.state_id=2 OR cs.state_id=4) 
GROUP BY `cs`.`code_id` HAVING (total<=4) 
UNION 
SELECT count(*) AS `total`, 
CASE c.state_id 
WHEN 3 THEN 'active' 
WHEN 5 THEN 'suspended' 
ELSE 'inactive' 
END AS `human_state`, `c`.* 
FROM `code_statuses` AS `cs` 
INNER JOIN `codes` AS `c` 
ON c.code_id = cs.code_id 
WHERE (cs.state_id=5) 
GROUP BY `cs`.`code_id` 
HAVING (total<4)

union 之前的部分是 $sq,之后的部分是 $sqtemp,两者的组合给出了上面的打印输出 他们两个都与 union in 是一回事

【问题讨论】:

  • 您使用的是哪个版本的 ZendFW? (string) $sq(string) $sqtemp 的输出分别是什么? (string) &lt;your union stmt 的输出是什么,最后:尝试传递 Zend_Db::SQL_UNION_ALL 常量,如果您使用的是 Zf1,我怀疑您正在使用
  • zend 版本是 1.12.31 有关 $sq 和 $sqtemp 的编辑见上文
  • Union all 给了我同样的错误 $sq=$this->_codes->getAdapter()->select() ->union(array( $sq, $sqtemp ),Zend_Db_Select::SQL_UNION_ALL ) ->order('cs.code_id'); SQLSTATE [42S22]:未找到列:1054 'order 子句'中的未知列 'c.code'
  • 只需将查询复制粘贴到mysql中(workbench,或者你有的其他工具,甚至cli都可以),然后分解,发现语法错误。我不是免费的调试器(我和其他人一样讨厌调试,但我们都必须这样做)
  • 原始 SQL 在 mysql 中运行良好,所以我怀疑它与 zend 框架有关

标签: php sql zend-framework zend-db


【解决方案1】:

再次查看您的代码后,我怀疑是 oder() 调用联合。您是通过cs.code_id 订购的,select 的任何声明中都没有提及,c.code_id 也没有提及。
尝试将c.code_idcs.code_id 添加到构成SELECT't 的UNION,可能考虑使用别名,然后您可以在order 子句中使用该别名。

$sq = $this->_codes->getAdapter()->select()
            ->from(array('cs' => 'code_statuses'),
                   array(
                       'total'     => 'count(*)'
                       'cscodeids' => 'code_ids',
                   ));
//...
$union = $this->_codes->getAdapter()
              ->select()
              ->union(array($sq,$sqtemp))
              ->order('cscodeids');

我相信,这应该可行。我从不同的地方获得了灵感。以下是一些指向我的答案的链接(无法全部找到 ATM):

【讨论】:

  • 谢谢,看看那些文件 $sq 应该是 $sqfinal=$this->_codes->getAdapter()->select() ->union(array($sq, $sqtemp)) ->order('cs.code_id');抱歉,
  • 我在页面底部发现了一些执行此操作的代码 $sq->order('c.code ASC');我把它注释掉了,我的查询运行得很好,但它可能会影响其他查询,会调查这个谢谢你的帮助,非常感谢:)
  • @user2764582:您可以尝试手动创建union 查询,因为as the manual says,要在构成UNION 的查询中使用ORDER BY,您必须使用括号: $sqfinal = '('.(string)$sq.') UNION ('.$sqtemp.')';...
猜你喜欢
  • 2011-11-19
  • 1970-01-01
  • 2012-11-07
  • 2013-03-01
  • 2021-12-28
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多