要求:
对关键词模糊匹配查询,并对结果按照外键分组,每个分组只取一条记录。。
排序方式是,先按匹配度排序,再按会员级别排序,最后按日期排序CREATE TABLE `aa` (
`nid` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` VARCHAR(45) CHARACTER SET utf8 COLLATE utf8_general_ci,
`title` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci,
`utype` INTEGER UNSIGNED,
`datetime` DATETIME,
PRIMARY KEY(`nid`)
)
ENGINE = MyISAM
CHARACTER SET utf8 COLLATE utf8_general_ci;insert into aa set uid='a',title='中国人民',utype='1',datetime='2008-07-01';
insert into aa set uid='a',title='中国',utype='1',datetime='2008-06-18';
insert into aa set uid='c',title='人民',utype='1',datetime='2008-06-19';
insert into aa set uid='b',title='中国人民',utype='2',datetime='2008-07-11';
insert into aa set uid='c',title='美国人民',utype='1',datetime='2008-07-02';
insert into aa set uid='d',title='人民',utype='2',datetime='2008-05-21';
insert into aa set uid='b',title='中国',utype='2',datetime='2008-07-10';
insert into aa set uid='e',title='美国',utype='2',datetime='2008-07-09';表数据如下:
nid uid title utype datetime
1 a 中国人民 1 2008-07-01
2 a 中国 1 2008-06-18
3 c 人民 1 2008-06-19
4 b 中国人民 2 2008-07-11
5 c 美国人民 1 2008-07-02
6 d 人民 2 2008-05-21
7 b 中国 2 2008-07-10
8 e 美国 2 2008-07-09做如下查询
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc查询记录:
1, 'a', '中国人民', '会员', '2008-07-01 00:00:00'
4, 'b', '中国人民', '普通', '2008-07-11 00:00:00'
5, 'c', '美国人民', '会员', '2008-07-02 00:00:00'
3, 'c', '人民', '会员', '2008-06-19 00:00:00'
2, 'a', '中国', '会员', '2008-06-18 00:00:00'
7, 'b', '中国', '普通', '2008-07-10 00:00:00'
6, 'd', '人民', '普通', '2008-05-21 00:00:00'对查询结果按uid分组,每个uid只取一条记录
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') group by utype order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc查询记录:
1, 'a', '中国人民', '会员', '2008-07-01 00:00:00'
4, 'b', '中国人民', '普通', '2008-07-11 00:00:00'这个结果不对从上个查询来看,结果应该包含四个分组a,b,c,d
请问这个sql实现问题出在哪里如果要取每个uid下的一条记录。。该怎么取呢。。多谢~~~
对关键词模糊匹配查询,并对结果按照外键分组,每个分组只取一条记录。。
排序方式是,先按匹配度排序,再按会员级别排序,最后按日期排序CREATE TABLE `aa` (
`nid` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` VARCHAR(45) CHARACTER SET utf8 COLLATE utf8_general_ci,
`title` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci,
`utype` INTEGER UNSIGNED,
`datetime` DATETIME,
PRIMARY KEY(`nid`)
)
ENGINE = MyISAM
CHARACTER SET utf8 COLLATE utf8_general_ci;insert into aa set uid='a',title='中国人民',utype='1',datetime='2008-07-01';
insert into aa set uid='a',title='中国',utype='1',datetime='2008-06-18';
insert into aa set uid='c',title='人民',utype='1',datetime='2008-06-19';
insert into aa set uid='b',title='中国人民',utype='2',datetime='2008-07-11';
insert into aa set uid='c',title='美国人民',utype='1',datetime='2008-07-02';
insert into aa set uid='d',title='人民',utype='2',datetime='2008-05-21';
insert into aa set uid='b',title='中国',utype='2',datetime='2008-07-10';
insert into aa set uid='e',title='美国',utype='2',datetime='2008-07-09';表数据如下:
nid uid title utype datetime
1 a 中国人民 1 2008-07-01
2 a 中国 1 2008-06-18
3 c 人民 1 2008-06-19
4 b 中国人民 2 2008-07-11
5 c 美国人民 1 2008-07-02
6 d 人民 2 2008-05-21
7 b 中国 2 2008-07-10
8 e 美国 2 2008-07-09做如下查询
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc查询记录:
1, 'a', '中国人民', '会员', '2008-07-01 00:00:00'
4, 'b', '中国人民', '普通', '2008-07-11 00:00:00'
5, 'c', '美国人民', '会员', '2008-07-02 00:00:00'
3, 'c', '人民', '会员', '2008-06-19 00:00:00'
2, 'a', '中国', '会员', '2008-06-18 00:00:00'
7, 'b', '中国', '普通', '2008-07-10 00:00:00'
6, 'd', '人民', '普通', '2008-05-21 00:00:00'对查询结果按uid分组,每个uid只取一条记录
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') group by utype order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc查询记录:
1, 'a', '中国人民', '会员', '2008-07-01 00:00:00'
4, 'b', '中国人民', '普通', '2008-07-11 00:00:00'这个结果不对从上个查询来看,结果应该包含四个分组a,b,c,d
请问这个sql实现问题出在哪里如果要取每个uid下的一条记录。。该怎么取呢。。多谢~~~
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') group by uid order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc查询记录:
1, 'a', '中国人民', '会员', '2008-07-01 00:00:00'
4, 'b', '中国人民', '普通', '2008-07-11 00:00:00'
3, 'c', '人民', '会员', '2008-06-19 00:00:00'
6, 'd', '人民', '普通', '2008-05-21 00:00:00'好。。现在进入正题。。
以上是我自己测试的一个例子但是在实际应用中,出现了一个意外。就是有一个会员的记录无论怎么都查询不到(在使用group by uid之前,他的记录在查询中是很靠前的,前三。但是使用group by uid之后就没有了)。这个意外,不知道什么原因,
我真正想问各位的是以上的sql实现是不是正确的。 。。或者有更好的实现办法呢
感谢wwwwb的指正也拜托各位指点
比如有两条uid=a的记录
1 a 中国人民 1 2008-07-01
2 a 中国 1 2008-06-18
记录1的匹配度是2,记录2的匹配度是1,如何保证按照where条件分组取最匹配数据
order by 是在 group 之后执行的吧。。那就是说order by只是对分组结果排序。。
如果是这样,谁能指点下。多谢~
匹配度是怎么得出的?
是这样的
在真实情况下,有一个uid=a的会员用户。。
在使用group by uid之前,关键词中国人民,会员用户a有多条记录,其中有四条标题含有“中国”、“人民”,另有多条含有“人民”,按说前者的匹配度是2,只含“人民”的匹配度是1。
在理论情况下,使用group by uid后,应该显示的是以上四条中的任意一条。但是真实的情况是,显示的是一条只含“人民”关键词的a会员记录。不可理解的是,这条信息无论关键词匹配度不是最高的,日期不是最近的,为什么group by uid的结果就是他呢
举例来说明如下:
uid=a的会员有如下几条记录:
nid uid title utype datetime
1 a 中国人民 1 2008-07-10
2 a 中国人民 1 2008-07-10
3 a 中国人民 1 2008-07-10
4 a 中国人民 1 2008-07-10
5 a 人民 1 2008-07-05
6 a 人民 1 2008-06-10
7 a 中国 1 2008-07-10
8 a 他国 1 2008-07-10
9 a 他国 1 2008-07-10
10 a 他国 1 2008-07-10
使用查询
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') group by uid order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc
关于uid=a的记录竟然是nid=5,这与期望出现的差别,为什么不是1~4中任意一个,而是5
所以,本人现在的疑惑在于,如果对于赋予group by条件,让其分组时获取的记录是匹配度最高的。
感谢~尚未解决。
对关键词模糊匹配查询,并对结果按照外键分组,每个分组只取一条记录。。
排序方式是,先按匹配度排序,再按会员级别排序,最后按日期排序
意思就是:关键词匹配查询,一个用户可能有多条记录,查询结果每个用户只返回一条记录。大家会怎么写呢?
帖上来参考下。我以上的sql方式测试没有问题,但是实际应用中就有问题。有好的办法没啊~~~
select nid,uid,title,case utype when 1 then '会员' when 2 then '普通' end as utype,datetime from aa where (title LIKE '%中国%' or title LIKE '%人民%') group by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0'), uid order by IF(title LIKE '%中国%','1','0')+IF(title LIKE '%人民%','1','0') desc,utype,datetime desc