左边用三个表连接mysql

时间:2013-11-01 11:08:46

标签: php mysql

我有三个表,我想要离开加入,但是两个表工作正常,当我添加第三个表时,它只显示第三个表中的那些记录

Select `0_debtor_trans_details`.debtor_trans_no As Bill_no,
  `0_debtor_trans_details`.stock_id As Item,
  `0_debtor_trans_details`.unit_price As Unit_price,
  `0_debtor_trans_details`.quantity As Qty,
  `0_debtor_trans_details`.description,
  `0_debtor_trans`.debtor_no As Reg_no,
  `0_debtor_trans`.type As type,
  `0_debtor_trans`.ov_amount As Total_Amount,
  `0_debtor_trans`.tran_date As Tran_Date,
  `0_comments`.memo_
From `0_debtor_trans_details`
  Inner Join `0_debtor_trans` On `0_debtor_trans_details`.debtor_trans_no =
    `0_debtor_trans`.trans_no
  Left Join `0_comments` On `0_debtor_trans`.trans_no = `0_comments`.id
Where `0_debtor_trans`.type = 10 And `0_comments`.type = 10
Group By `0_debtor_trans_details`.debtor_trans_no,
  `0_debtor_trans_details`.stock_id,
  `0_debtor_trans_details`.unit_price,
  `0_debtor_trans_details`.quantity,
  `0_debtor_trans_details`.description,
  `0_debtor_trans`.debtor_no,
  `0_debtor_trans`.type,
  `0_debtor_trans`.ov_amount,
  `0_debtor_trans`.tran_date,
  `0_comments`.memo_
Having `0_debtor_trans_details`.description = 'Fine'      

当我添加0_comments时,它仅显示0_comments中的那些记录 怎么做

CREATE TABLE IF NOT EXISTS `0_comments` (
  `type` int(11) NOT NULL DEFAULT '0',
  `id` int(11) NOT NULL DEFAULT '0',
  `date_` date DEFAULT '0000-00-00',
  `memo_` tinytext,
  KEY `type_and_id` (`type`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `0_debtor_trans` (
  `trans_no` int(11) unsigned NOT NULL DEFAULT '0',
  `type` smallint(6) unsigned NOT NULL DEFAULT '0',
  `version` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `debtor_no` int(11) unsigned DEFAULT NULL,
  `branch_code` int(11) NOT NULL DEFAULT '-1',
  `tran_date` date NOT NULL DEFAULT '0000-00-00',
  `due_date` date NOT NULL DEFAULT '0000-00-00',
  `reference` varchar(60) NOT NULL DEFAULT '',
  `tpe` int(11) NOT NULL DEFAULT '0',
  `order_` int(11) NOT NULL DEFAULT '0',
  `ov_amount` double NOT NULL DEFAULT '0',
  `ov_gst` double NOT NULL DEFAULT '0',
  `ov_freight` double NOT NULL DEFAULT '0',
  `ov_freight_tax` double NOT NULL DEFAULT '0',
  `ov_discount` double NOT NULL DEFAULT '0',
  `alloc` double NOT NULL DEFAULT '0',
  `rate` double NOT NULL DEFAULT '1',
  `ship_via` int(11) DEFAULT NULL,
  `dimension_id` int(11) NOT NULL DEFAULT '0',
  `dimension2_id` int(11) NOT NULL DEFAULT '0',
  `payment_terms` int(11) DEFAULT NULL,
  `Datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deposit` varchar(30) NOT NULL DEFAULT 'Deposit',
  `payment` varchar(30) NOT NULL DEFAULT 'Payment',
  `Date` date NOT NULL DEFAULT '0000-00-00',
  PRIMARY KEY (`type`,`trans_no`),
  KEY `debtor_no` (`debtor_no`,`branch_code`),
  KEY `tran_date` (`tran_date`),
  KEY `ov_amount` (`ov_amount`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `0_debtor_trans_details` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `debtor_trans_no` int(11) DEFAULT NULL,
  `debtor_trans_type` int(11) DEFAULT NULL,
  `stock_id` varchar(20) NOT NULL DEFAULT '',
  `description` tinytext,
  `unit_price` double NOT NULL DEFAULT '0',
  `unit_tax` double NOT NULL DEFAULT '0',
  `quantity` double NOT NULL DEFAULT '0',
  `discount_percent` double NOT NULL DEFAULT '0',
  `standard_cost` double NOT NULL DEFAULT '0',
  `qty_done` double NOT NULL DEFAULT '0',
  `src_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `Transaction` (`debtor_trans_type`,`debtor_trans_no`),
  KEY `src_id` (`src_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12523 ;

1 个答案:

答案 0 :(得分:2)

也许这就是你所追求的......

SELECT DISTINCT d.debtor_trans_no Bill_no
              , d.stock_id Item
              , d.unit_price Unit_price
              , d.quantity Qty
              , d.description
              , t.debtor_no Reg_no
              , t.type 
              , t.ov_amount Total_Amount
              , t.tran_date Tran_Date
              , c.memo_
           FROM `0_debtor_trans_details` d
           JOIN `0_debtor_trans` t 
             ON t.trans_no = d.debtor_trans_no 
           JOIN `0_comments` c
             ON c.id = t.trans_no 
          WHERE t.type = 10 
            AND c.type = 10
            AND d.description = 'Fine'
          ORDER 
             BY d.debtor_trans_no
              , d.stock_id
              , d.unit_price
              , d.quantity
              , d.description
              , t.debtor_no
              , t.type
              , t.ov_amount
              , t.tran_date
              , c.memo_;