简体   繁体   中英

left join mysql with three table

I have three table which i want to left join but two table is working perfectly and when i add 3rd table it shows only those records which are in 3rd table

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'      

When i add 0_comments it shows only those records which are in 0_comments How to do this

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 ;

Perhaps this is what you're after...

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_;    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM