我很难查询。这是在rails(2.1)中,但也许我需要一个find_by_sql。
我有一张公司表,里面有很多客户,有很多组。我需要找到在一定时间内(比如 3 个月)内没有任何客户的公司。
我想出的最好的是:
Company.all(:include => { :clients => :groups },
:conditions => ["(SELECT count(*) FROM groups WHERE
groups.client_id = clients.id AND clients.company_id = companies.id
AND groups.created_at > ?) = 0 AND companies.is_active = 1 AND
clients.is_active = 1", 3.months.ago])
理想情况下,这也会返回为每家公司制作的最后一组的时间(不知道从哪里开始)。为此,我一直在为每家公司使用单独的查询:
Group.last(:include => { :client => :company },
:conditions => { "companies.id" => company.id })
当我运行报告时,我得到的公司在上个月左右创建了组,所以看起来我的初始查询不正确。但我不知道从这里去哪里。
编辑:这是表的表创建语句。我确实删除了大部分字段并留下了相关的内容。我希望这行得通。
mysql> show create table companies;
| Table | Create Table | | companies | CREATE TABLE `companies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_on` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1646 DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> show create table clients;
| Table | Create Table | clients | CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) DEFAULT NULL,
`created_on` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `CompanyID` (`company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3374 DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)
mysql> show create table groups;
| Table | Create Table | groups | CREATE TABLE `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ClientID` (`client_id`)
) ENGINE=InnoDB AUTO_INCREMENT=157006 DEFAULT CHARSET=latin1 |
1 row in set (0.00 sec)