2011-04-17 46 views
2

我喜欢解决以下问题。在我的数据库中有不同的链接到日期的字符串。系统运行时,每个日期将有大约三到二十个字符串链接到它。找到最匹配的字符串列表

现在我想用一个字符串列表向数据库发送查询。数据库应该返回与查询共有最多字符串的日期。

我做了一个示例数据库,您可以使用附加到此文章的SQL脚本创建。在这个示例中,我的查询将包含字符串“abc”, “yz”, “def”, “jkl”。数据库应该根据匹配的数量来排列日期。这意味着(100, '2005-05-05 05:05:05')应该是第一个,而(103, '2008-08-08 08:08:08')应该是最后一个。没有必要根据另一个标准进一步对具有等量匹配字符串的字符串进行排序。

我正在运行MySQL 5.5.11。我怎样才能达到这个目标?

CREATE TABLE sampledate(
     id INT NOT NULL PRIMARY KEY, 
     time TIMESTAMP NOT NULL); 

CREATE TABLE properties(
    idprop INT NOT NULL PRIMARY KEY, 
    property VARCHAR(45) NOT NULL, 
    iddate INT NOT NULL, 
    FOREIGN KEY (iddate) REFERENCES sampledate(id) 
); 

INSERT INTO sampledate (id, time) VALUES (100, '2005-05-05 05:05:05'); 
INSERT INTO sampledate (id, time) VALUES (101, '2006-06-06 06:06:06'); 
INSERT INTO sampledate (id, time) VALUES (102, '2007-07-07 07:07:07'); 
INSERT INTO sampledate (id, time) VALUES (103, '2008-08-08 08:08:08'); 

INSERT INTO properties (idprop, property, iddate) VALUES (201, "abc", 100); 
INSERT INTO properties (idprop, property, iddate) VALUES (202, "def", 100); 
INSERT INTO properties (idprop, property, iddate) VALUES (203, "ghi", 100); 
INSERT INTO properties (idprop, property, iddate) VALUES (204, "jkl", 100); 

INSERT INTO properties (idprop, property, iddate) VALUES (301, "def", 101); 
INSERT INTO properties (idprop, property, iddate) VALUES (302, "mno", 101); 
INSERT INTO properties (idprop, property, iddate) VALUES (303, "pqr", 101); 
INSERT INTO properties (idprop, property, iddate) VALUES (304, "stu", 101); 
INSERT INTO properties (idprop, property, iddate) VALUES (305, "vwx", 101); 

INSERT INTO properties (idprop, property, iddate) VALUES (401, "vwx", 102); 
INSERT INTO properties (idprop, property, iddate) VALUES (402, "pqr", 102); 
INSERT INTO properties (idprop, property, iddate) VALUES (403, "ghi", 102); 
INSERT INTO properties (idprop, property, iddate) VALUES (404, "mno", 102); 

INSERT INTO properties (idprop, property, iddate) VALUES (501, "vwx", 103); 
INSERT INTO properties (idprop, property, iddate) VALUES (502, "mno", 103); 
INSERT INTO properties (idprop, property, iddate) VALUES (503, "yz", 103); 
+1

我不认为102应该在结果集中。你可能是指103. – 2011-04-17 11:54:26

+0

我对这个阶段感到困惑,“根据匹配字符串的数量”。这是否表明,当没有任何匹配时,你想要某种模糊匹配和/或包含日期? – 2011-04-17 12:03:54

+1

'properties.property'列是否可以为任何单个'iddate'重复?如果是这样,是否应该应用不同的计数? – 2011-04-17 12:07:15

回答

1

这给你的匹配数量。

select iddate, count(iddate) as num_dates 
from properties 
where property in ('abc', 'yz', 'def', 'jkl') 
group by iddate 
order by num_dates desc 

要获取日期数据,只需加入该查询。

select t1.*, t2.num_dates 
from sampledate t1 
inner join 
    (select iddate, count(iddate) as num_dates 
    from properties 
    where property in ('abc', 'yz', 'def', 'jkl') 
    group by iddate) t2 
on t1.id = t2.iddate 
order by num_dates desc 

如果计数确实需要根据属性的数量,因为该表允许一个日期有许多行,其中的属性=“ABC”,那么你只指望该列,而不是' iddate”。

select t1.*, t2.num_props 
from sampledate t1 
inner join 
    (select iddate, count(distinct property) as num_props 
    from properties 
    where property in ('abc', 'yz', 'def', 'jkl') 
    group by iddate) t2 
on t1.id = t2.iddate 
order by num_props desc ;