簡體   English   中英

Zend pdo_oci和pdo_mysql生成不同的查詢格式

[英]Zend pdo_oci and pdo_mysql generating different query formats

Oracle數據庫適配器:“ pdo_oci ” MySQL數據庫適配器:“ pdo_mysql

用於建立JOIN查詢的代碼:

            $db = Zend_Db_Table::getDefaultAdapter();
            $db->setFetchMode(Zend_Db::FETCH_OBJ);

            $select = $db->select()->from(array(
                   't1' => $this->_Table1
            ));
            $select = $select->join(array(
                   't2' => $this->_Table2
           ), 't1.MENU_ID = t2.SUBMENU_ID',
            array(
                    'MENU_ID',
                    'SET_REDIRECT_URL',
                    'SUBMENU_TYPE'
            ));
            $select = $select->order(array(
                    'MENU_ID',
                    't2.DISPLAY_ORDER'
            ));
            $select = $select->where('t1.IS_ACTIVE = ?', 'Y');
            $result = $db->fetchAll($select);

由Oracle適配器(pdo_oci)生成的查詢:

SELECT "t1".*, "t2"."MENU_ID" AS "parent_id", "t2"."SET_REDIRECT_URL", "t2"."SUBMENU_TYPE" FROM "DRP_ADMIN_MENU_MASTER" "t1"  INNER JOIN "DRP_ADMIN_MENU_MAPPER" "t2" ON t1.MENU_ID = t2.SUBMENU_ID WHERE (t1.IS_ACTIVE = 'Y') ORDER BY "parent_id" ASC, "t2"."DISPLAY_ORDER" ASC

由Mysql適配器(pdo_mysql)生成的查詢:

SELECT `t1`.*, `t2`.`MENU_ID` AS `parent_id`, `t2`.`SET_REDIRECT_URL`, `t2`.`SUBMENU_TYPE` FROM `DRP_ADMIN_MENU_MASTER` AS `t1`  INNER JOIN `DRP_ADMIN_MENU_MAPPER` AS `t2` ON t1.MENU_ID = t2.SUBMENU_ID WHERE (t1.IS_ACTIVE = 'Y') ORDER BY `parent_id` ASC, `t2`.`DISPLAY_ORDER` ASC

問題:除非使用雙引號,否則Oracle DB無法在[t1.MENU_ID = t2.SUBMENU_ID]中標識t1和t2。 而且,如果我手動輸入雙引號,則mysql會出錯。

Oracle中的錯誤:

常規錯誤:904 OCIStmtExecute:ORA-00904:“ T2”。“ SUBMENU_ID”:無效的標識符

解決了。 使用$ db-> quoteIdentifier()。 而且有效

        $db = Zend_Db_Table::getDefaultAdapter();
        $db->setFetchMode(Zend_Db::FETCH_OBJ);

        $select = $db->select()->from(array(
               't1' => $this->_Table1
        ));
        $select = $select->join(array(
               't2' => $this->_Table2
       ), $db->quoteIdentifier('t1.MENU_ID') . ' = ' . $db->quoteIdentifier('t2.SUBMENU_ID'),
        array(
                'MENU_ID',
                'SET_REDIRECT_URL',
                'SUBMENU_TYPE'
        ));
        $select = $select->order(array(
                'MENU_ID',
                't2.DISPLAY_ORDER'
        ));
        $select = $select->where($db->quoteIdentifier('t1.IS_ACTIVE') . ' = ?', 'Y');
        $result = $db->fetchAll($select);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM