您好,欢迎访问一九零五行业门户网

zend_db_table_abstract 中使用 zend_db_select 和join, Join Le

今天我们来讲一下关于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 = $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
  }
}
其它类似信息

推荐信息