Use Zend_db_select and join in Zend_db_table_abstract, join left

Source: Internet
Author: User
The code is as follows Copy Code


--
--The structure of the table ' Charge_logs '
--

CREATE TABLE IF not EXISTS ' charge_logs ' (
' id ' int (one) not NULL auto_increment,
' charge_id ' int (one) is not NULL,
' title ' Text not NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=2;

--
--the data in the Dump table ' Charge_logs '
--

INSERT into ' charge_logs ' (' id ', ' charge_id ', ' title ') VALUES
(1, 1, ' XXXXXXX ');

--
--The structure of the table ' User_charges '
--

CREATE TABLE IF not EXISTS ' user_charges ' (
' ID ' int (a) unsigned not NULL auto_increment,
' user_id ' int (a) 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 not NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 auto_increment=2;

--
--the data in the Dump table ' 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,, ' 2011-09-09 00:00:00 ', ' 2011-09-16 00:00:00 ', ' 0000-00-00 00:00:00 ', ' PayPal ');
Using a select query in Zend_db_table_abstract

$select = $this->getadapter ()->select ();
$select->from (' UC ' => ' user_charges '))
->joinleft (' cl ' => ' charge_logs '), ' uc.id = cl.charge_id ', array (' title '))
->where (' uc.user_id=? ', $id);
return $this->getadapter ()->fetchall ($select);
If you are using a mapper, you can use this

$select = $this->getdbtable ()->getadapter ()->select ();
$select->from (' UC ' => ' user_charges '))
->joinleft (' cl ' => ' charge_logs '), ' uc.id = cl.charge_id ', array (' title '))
->where (' uc.user_id=? ', $id);

return $this->getdbtable ()->getadapter ()->fetchall ($select);
Output results

Array (1) {
  [0] => Array {
    [id] => string (1) "1"
    [ user_id "] => string (1)" 1 "
    [" Charge_type "] => string (1)" 1 "
    [" Charge _subtype "] => string (1)" 1 "
    [" Charge_credits "] => string (3)" M "
    [" Buy_date "] => string (a)" 2011-09-09 00:00:00 "
    [" valid_to "] => string (19)" 2011-09-16 00:00 : "
    [next_charge_date] => string" 0000-00-00 00:00:00
    [" Payment "] => string (6)" PayPal "
    [" title "] => string (7)" XXXXXXX "
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.