我搜索过,我无法找到答案。这里存在其他链接,但它们似乎并不相关,因为它们仅引用一个或两个表,一个静态已知的日期对于所有记录都是相同的,或者被标记为已解决而没有给出真正的答案。
我有以下表格:
-- list of items
CREATE TABLE `a` (
`a_id` bigint(20) unsigned NOT NULL auto_increment primary key,
`a_name` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=176 DEFAULT CHARSET=utf8;
-- sample data
insert into a(a_id,a_name) values('1','apple');
insert into a(a_id,a_name) values('2','banana');
insert into a(a_id,a_name) values('3','cheese');
insert into a(a_id,a_name) values('4','bucket of lard');
-- places
CREATE TABLE `b` (
`b_id` bigint(20) unsigned NOT NULL auto_increment primary key,
`b_data` varchar(20) not null
)ENGINE=InnoDB AUTO_INCREMENT=629 DEFAULT CHARSET=utf8;
insert into b(b_id,b_data) values('1','floor');
insert into b(b_id,b_data) values('2','counter top');
insert into b(b_id,b_data) values('3','bed');
insert into b(b_id,b_data) values('4','table');
-- item data showing item A is in place B, with extra data.
CREATE TABLE `c` (
`c_id` bigint(20) unsigned NOT NULL auto_increment primary key,
`c_a_id` bigint(20) unsigned NOT NULL,
FOREIGN KEY (c_a_id) REFERENCES a(a_id),
`c_b_id` bigint(20) unsigned NOT NULL,
FOREIGN KEY (c_b_id) REFERENCES b(b_id),
`c_value` decimal(60,2) NOT NULL default '0.00',
`c_when_created` bigint(20) unsigned NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=129886 DEFAULT CHARSET=utf8;
-- sample data (by no means complete!)
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('1','1','1', '3.14578', UNIX_TIMESTAMP('2011-02-22 03:00:00'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('2','2','1', '14578', UNIX_TIMESTAMP('2011-02-22 03:00:01'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('3','3','1', '4578', UNIX_TIMESTAMP('2011-02-22 03:00:02'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('4','4','2', '578', UNIX_TIMESTAMP('2011-02-22 03:50:03'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('1','1','4', '78', UNIX_TIMESTAMP('2011-02-22 03:00:00'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('2','2','3', '8', UNIX_TIMESTAMP('2011-02-22 03:01:01'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('3','3','2', '3', UNIX_TIMESTAMP('2011-02-22 03:00:42'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('4','4','1', '31', UNIX_TIMESTAMP('2011-02-22 03:05:03'));
insert into c(c_id, c_a_id, c_b_id, c_value, c_when_created )
values('1','1','1', '77', UNIX_TIMESTAMP('2006-03-27 03:10:00'));
现在的问题是这样的:我想搜索表'c'中的'a'中的项目作为就位表'b',但是只返回由于具有过时而未过期的记录存在引用此数据的较新记录。在我看来,“c”中具有最新日期列出的“a”和“b”的字段是唯一有效的字段,其余字段是旧的并且过时并保留用于历史数据跟踪和报告。
如果我在以下内容中查找了所有内容,则给出c中的以下数据的示例:
insert into c(c_a_id, c_b_id, c_value, c_when_created )
values('1','1', '3.14578', UNIX_TIMESTAMP('2011-02-22 03:00:00'));
insert into c(c_a_id, c_b_id, c_value, c_when_created )
values('2','1', '14578', UNIX_TIMESTAMP('2011-02-22 03:00:01'));
insert into c(c_a_id, c_b_id, c_value, c_when_created )
values('1','1', '4578', UNIX_TIMESTAMP('2011-02-22 03:00:02'));
..我希望只有最后两行的日期'2011-02-22 03:00:01'和'2011-02-22 03:00:02'作为'2011-02- 22 03:00:00'行将被视为'旧',因为'2011-02-22 03:00:02'行存在。
IE,如果a_id等于'1'存在4条记录,并且我在c中搜索该记录,期望b_id为2,我只想得到一条日期最近的记录。
将数据添加到c,以一种很好的方式引用a和b一直是一种乐趣,并将a和b中的项目数量保持在可管理的水平;但要证明它很难恢复。因为我只想返回系统中没有新记录的记录。
答案 0 :(得分:0)
如果我正确理解您的问题,您可以按日期降序进行选择排序并将结果限制为1
select b.*, c.value, c.date from b
inner join c on c_b_id = b.b_id
order by c.date desc
limit 1