人正做尝试做一个搜索功能,碰到如下问题,百思不得其解
有两个表:
第一个表:grp(grpname,grptags,grpcat_id) 中文: 小组(组名,标签,分类ID)
第二个表:grpcat(grpcat_id,grpcatname) 中文: 小组分类(分类ID,类名)
现在我要搜索关于‘上海的信息’,对应的搜索列是grp表格中的grpname,grptags(组名和标签)两个字段,和grpcat表中的grpcatname(类名)
我的sql语句如下:
SELECT grp.grpname, grp.grptags, grpcat.grpcatname
FROM grp, grpcat
WHERE grp.grpcat_id = grpcat.grpcat_id
AND (
grp.grpname LIKE '%上海%'
OR grp.grptags LIKE '%上海%'
OR grpcat.grpcatname LIKE '%上海%'
)
mysql并没有报错,但是就是没有数据。我删除了一些条件,觉得可能是跨表like的问题,单独搜索grp表中的数据没有问题,但是牵涉到grpcat(小组分类),就找不出来了,望指教,或者给个思路。谢谢!
有两个表:
第一个表:grp(grpname,grptags,grpcat_id) 中文: 小组(组名,标签,分类ID)
第二个表:grpcat(grpcat_id,grpcatname) 中文: 小组分类(分类ID,类名)
现在我要搜索关于‘上海的信息’,对应的搜索列是grp表格中的grpname,grptags(组名和标签)两个字段,和grpcat表中的grpcatname(类名)
我的sql语句如下:
SELECT grp.grpname, grp.grptags, grpcat.grpcatname
FROM grp, grpcat
WHERE grp.grpcat_id = grpcat.grpcat_id
AND (
grp.grpname LIKE '%上海%'
OR grp.grptags LIKE '%上海%'
OR grpcat.grpcatname LIKE '%上海%'
)
mysql并没有报错,但是就是没有数据。我删除了一些条件,觉得可能是跨表like的问题,单独搜索grp表中的数据没有问题,但是牵涉到grpcat(小组分类),就找不出来了,望指教,或者给个思路。谢谢!
a.grpname,
a.grptags,
b.grpcatname
FROM grp a, grpcat b
WHERE a.grpcat_id = b.grpcat_id --#1.确定在这个条件下,记录数据大于0
AND
(
a.grpname LIKE N'%上海%'
OR a.grptags LIKE N'%上海%'
OR b.grpcatname LIKE N'%上海%' --#2.加上N试试
)
两个表没有对应的id造成的
试改为左连接试试SELECT grp.grpname, grp.grptags, grpcat.grpcatname
FROM grp left join grpcat
on grp.grpcat_id = grpcat.grpcat_id
where (
grp.grpname LIKE '%上海%'
OR grp.grptags LIKE '%上海%'
OR grpcat.grpcatname LIKE '%上海%'
)
哎,那么傻的错误。