1
我有两个表:显示的数据,加入了与信息从表2
rankHistory存储反对它给关键字BOOKID的排名。 它的结构如下:
create table rankHistory (
id int(10),
rankUpdated timestamp,
bookId varchar(10),
keyword varchar(100),
rank int(5)
);
包含在此表中实施例的数据:
INSERT INTO rankHistory
(`id`, `rankUpdated`,`bookId`,`keyword`,`rank`)
VALUES
(1, '2016-05-27 05:11:44', 'BK-001','romantic','80'),
(2, '2016-05-28 08:11:44', 'BK-001','romantic','15'),
(3, '2016-05-28 09:11:44', 'BK-001','romantic','13'),
(4, '2016-05-27 07:11:44', 'BK-001','romantic book','77'),
(5, '2016-05-28 08:11:44', 'BK-001','romantic book','25'),
(6, '2016-05-28 09:11:44', 'BK-001','romantic book','22'),
(7, '2016-05-27 05:11:44', 'BK-002','horror','65'),
(8, '2016-05-28 08:11:44', 'BK-002','horror','10'),
(9, '2016-05-29 07:11:44', 'BK-002','horror book','25'),
(10, '2016-05-29 08:11:44', 'BK-002','horror book','9')
;
bookConfig包含它的给定的关键字的BOOKID的信息。 它的结构如下:
create table bookConfig (
id int(10),
category varchar(40),
author varchar(255),
info varchar (255)
);
包含在此表中实施例的数据:
INSERT INTO bookConfig
(`id`,`bookId`,`keyword`, `category`, `author`,`info`)
VALUES
(1, 'BK-001', 'romantic', 'Romance', 'Author1','Romance themed book written by author1'),
(2, 'BK-001', 'romantic book', 'Romance', 'Author1','Romance themed book written by author1'),
(3, 'BK-002', 'horror ', 'Horror', 'Author2','Horror themed book written by author2'),
(4, 'BK-002', 'horror book', 'Horror', 'Author2','Horror book written by author2')
;
我在http://sqlfiddle.com/#!9/9502a9合并于此。
该表排名历史包含成千上万的记录类似于提供的数据,我的关注也与速度。结构可能从一开始就是错误的,但这是遗留下来的,我不得不与之合作。
我想实现如下:
选择与最新的时间戳的条目每个独特BOOKID /关键字组合,然后加入与包含在bookConfig该组合的信息。
因此所需的输出将是:
rankUpdated bookId keyword rank,category,author, info
----------------------------------------------------------------------------
2016-05-28 09:11:44, BK-001, romantic, 13, romance, Author1, Romance themed book..
2016-05-28 09:11:44, BK-001, romantic book, 22, romance, Author1, Romance themed book..
2016-05-28 08:11:44, BK-002, horror, 10, horror, Author2, Horror themed book..
2016-05-29 08:11:44, BK-002, horror book, 9, horror, Author2, Horror book ...
我曾尝试几种方法来完成这个没有成功和我失去了如何实现这一正确。如果有更熟悉mySQL的人可以指出实现这一目标的最佳方式,我将非常感激。
提前很多感谢分享您的专业知识
先生,您的查询工作完美,并返回所需的数据很快,以及。非常感谢您的解决方案 - 令人惊叹! – datether
...但不如[这个人](http://stackoverflow.com/users/1144035/gordon-linoff)...很高兴我可以帮助你。 –