萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> zend_db_table_abstract 中使用 zend_db_select 和join, Join Left

zend_db_table_abstract 中使用 zend_db_select 和join, Join Left

今天我們來講一下關於zend_db_table_abstract中怎麼使用一些查詢語句,有需要的朋友可以參考一下。  代碼如下 復制代碼


--
-- 表的結構 `charge_logs`
--
 
CREATE TABLE IF NOT EXISTS `charge_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `charge_id` int(11) NOT NULL,
  `title` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
 
--
-- 轉存表中的數據 `charge_logs`
--
 
INSERT INTO `charge_logs` (`id`, `charge_id`, `title`) VALUES
(1, 1, 'XXXXXXX');
 
--
-- 表的結構 `user_charges`
--
 
CREATE TABLE IF NOT EXISTS `user_charges` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) NOT NULL,
  `charge_type` int(3) NOT NULL,
  `charge_subtype` int(3) NOT NULL,
  `charge_credits` int(3) NOT NULL,
  `buy_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `valid_to` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `next_charge_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `payment` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
 
--
-- 轉存表中的數據 `user_charges`
--
 
INSERT INTO `user_charges` (`id`, `user_id`, `charge_type`, `charge_subtype`, `charge_credits`, `buy_date`, `valid_to`, `next_charge_date`, `payment`) VALUES
(1, 1, 1, 1, 100, '2011-09-09 00:00:00', '2011-09-16 00:00:00', '0000-00-00 00:00:00', 'paypal');
在zend_db_table_abstract中使用select 查詢

$select = $this->getAdapter()->select();
$select->from(array('uc' => 'user_charges'))
 ->joinLeft(array('cl' => 'charge_logs'), 'uc.id = cl.charge_id', array('title'))
 ->where('uc.user_id=?', $id);
return $this->getAdapter()->fetchAll($select);
如果是用了Mapper,可以這樣使用

$select = $this->getDbTable()->getAdapter()->select();
$select->from(array('uc' => 'user_charges'))
 ->joinLeft(array('cl' => 'charge_logs'), 'uc.id = cl.charge_id', array('title'))
 ->where('uc.user_id=?', $id);
 
return $this->getDbTable()->getAdapter()->fetchAll($select);
輸出結果

array(1) {
  [0] => array(10) {
    ["id"] => string(1) "1"
    ["user_id"] => string(1) "1"
    ["charge_type"] => string(1) "1"
    ["charge_subtype"] => string(1) "1"
    ["charge_credits"] => string(3) "100"
    ["buy_date"] => string(19) "2011-09-09 00:00:00"
    ["valid_to"] => string(19) "2011-09-16 00:00:00"
    ["next_charge_date"] => string(19) "0000-00-00 00:00:00"
    ["payment"] => string(6) "paypal"
    ["title"] => string(7) "XXXXXXX"
  }
}

copyright © 萬盛學電腦網 all rights reserved