我的查询工作正常。
$stmt = "select t.tax_name, "
. "(select truncate( sum((i.item_tax / 100) * i.item_qty_total), 2 ) from {CI}invoice_items i "
. "where i.tax_id = t.tax_id $qs ) total_tax, "
. "(select truncate( sum(i.item_qty_total), 2 ) from {CI}invoice_items i "
. "where i.tax_id = t.tax_id $qs ) turnover "
. "from {CI}taxes t ";
但问题是我必须根据发票状态跳过invoice_items,我试试这样
$stmt = "select t.tax_name, "
. "(select truncate( sum((i.item_tax / 100) * i.item_qty_total), 2 ) from {CI}invoice_items i "
. "left join {CI}invoices inv on inv.invoice_id = i.invoice_id and inv.status not like 'cancel%' "
. "where i.tax_id = t.tax_id $qs ) total_tax, "
. "(select truncate( sum(i.item_qty_total), 2 ) from {CI}invoice_items i "
. "left join {CI}invoices inv on inv.invoice_id = i.invoice_id and inv.status not like 'cancel%' "
. "where i.tax_id = t.tax_id $qs ) turnover "
. "from {CI}taxes t ";
但是连接不会影响结果,它显示的结果与第一个查询提供的结果相同。我的JOIN有什么问题?
以下是invoice_items
CREATE TABLE `ci_invoice_items` (
`ii_id` bigint(10) NOT NULL AUTO_INCREMENT COMMENT 'INVOICE_ITEM_ID',
`invoice_id` bigint(10) NOT NULL,
`customer_id` bigint(10) NOT NULL,
`item_id` bigint(10) NOT NULL,
`invoice_date` datetime NOT NULL,
`item_price` decimal(10,2) NOT NULL,
`item_unit` varchar(255) NOT NULL,
`item_qty` decimal(7,2) NOT NULL,
`item_qty_total` decimal(10,2) NOT NULL COMMENT 'TOTAL_AMOUNT_INCLUDING_QUANTITY',
`item_tax` decimal(10,2) NOT NULL,
`item_tax_total` decimal(10,2) NOT NULL COMMENT 'TOTAL_AMOUNT_INCLUDING_TAX',
`tax_id` int(2) NOT NULL,
`item_description` mediumtext NOT NULL,
PRIMARY KEY (`ii_id`),
KEY `invoice_id` (`invoice_id`,`customer_id`,`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=170 DEFAULT CHARSET=utf8
和invoices
CREATE TABLE `ci_invoices` (
`invoice_id` bigint(10) NOT NULL AUTO_INCREMENT,
`customer_id` bigint(10) NOT NULL,
`tmp_inv_number` int(10) NOT NULL,
`inv_prefix` varchar(50) NOT NULL,
`invoice_type` varchar(20) NOT NULL,
`invoice_number` varchar(255) NOT NULL,
`order_id` int(10) NOT NULL,
`inter_state` varchar(10) NOT NULL,
`form` varchar(50) NOT NULL,
`order_date` datetime NOT NULL,
`ref_number` varchar(200) NOT NULL,
`com_ref_per` varchar(150) NOT NULL COMMENT 'COMPANY_REFF_NUMBER',
`com_ref_num` varchar(50) NOT NULL COMMENT 'COMPANY_REFF_NAME',
`due_date` datetime NOT NULL,
`invoice_date` datetime NOT NULL,
`invoice_note` mediumtext NOT NULL,
`shipping_address` text NOT NULL,
`tax_id` int(5) NOT NULL,
`shipping` decimal(9,2) NOT NULL,
`total_amount` decimal(18,2) NOT NULL,
`round_off_amt` decimal(5,2) NOT NULL COMMENT 'ROUND_OFF_AMOUNT',
`paid_amount` decimal(18,2) NOT NULL,
`c_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'CURRENT_DATE',
`made_by` varchar(100) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT 'unpaid',
`reset_status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`invoice_id`),
KEY `customer_id` (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=158 DEFAULT CHARSET=utf8