首先我要選擇bill_client
和一列形式bill_pkg
一些列嵌套查詢來選擇,這就是簡單的象下面這樣:無法使用在where子句
查詢:
SELECT c.company_id, c.client_id, p.pkg_rate
FROM bill_client c
JOIN bill_pkg p on c.pkg_id=p.pkg_id
WHERE c.status=1
我其實是想選擇與其中extract(year_month from curdate())>
SELECT c.company_id, c.client_id, p.pkg_rate
FROM bill_client c
JOIN bill_pkg p on c.pkg_id=p.pkg_id
WHERE c.status=1 and extract(year_month from curdate())>
(SELECT MAX(due_month)
FROM bill_rent
WHERE c.client_id=client_id and c.company_id=company_id
GROUP BY client_id, company_id)
但是這個查詢只返回那些ro w其中client_id
與bill_rent
表匹配,並非來自bill_client
表的所有行。我需要檢查company_id
與client_id
存在但bill_rent
表due_month
本月列。請任何幫助。
這裏的表模式:
tabll:bill_client
CREATE TABLE `bill_client` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` varchar(30) NOT NULL,
`client_name` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`opening_balance` decimal(10,2) NOT NULL,
`conn_charge` decimal(13,2) NOT NULL,
`con_date` date NOT NULL,
`mobile` varchar(12) NOT NULL,
`pkg_id` int(11) NOT NULL,
`conn_type` int(11) NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1',
`area_id` int(11) NOT NULL,
`managed_by` int(11) NOT NULL,
`remarks` varchar(100) NOT NULL,
`photo` varchar(50) DEFAULT NULL,
`company_id` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=165 DEFAULT CHARSET=utf8
表:bill_pkg
CREATE TABLE `bill_pkg` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pkg_id` int(11) NOT NULL,
`pkg_name` varchar(10) NOT NULL,
`pkg_rate` decimal(10,0) NOT NULL,
`company_id` varchar(15) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `pkg_name` (`pkg_name`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
表:bill_rent
CREATE TABLE `bill_rent` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` varchar(15) NOT NULL,
`client_id` varchar(10) NOT NULL,
`due_month` varchar(6) NOT NULL,
`pay_date` date NOT NULL,
`amount_due` decimal(10,2) NOT NULL,
`amount_paid` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
對不起,我不明白你在做什麼。請提供一些樣本數據,基於樣本數據的預期結果以及您的查詢產生的實際結果。 – Shadow