问题描述
在某些情况下,我在使用像关键字一样的mysql时遇到问题.我的要求是这样的.首先,当我搜索'ABC'
时,结果应找到ABC
和ABCdef
,但不能找到xyzABCdef
或xyzABC
.起初使用ABC%
似乎很简单.但是在我搜索'heart%'
的情况下,它不返回具有'liver heart'
的行,因为它仅返回字符串开头具有heart
的行.然后我尝试使用% heart%
.此返回的行具有,但不是在字符串开头有心脏的那些行.我有点卡在这里..所以伙计们帮帮我.
I am having problem in using mysql like keyword in certain condition. My requirement goes like this.First, when i search for 'ABC'
, the result should find ABC
and ABCdef
but not xyzABCdef
or xyzABC
. It seems simple at first to use ABC%
. but in condition when i search for 'heart%'
, it doesnot return row that have 'liver heart'
because it returns only row that have heart
at the beginning of the string.Then i tried using % heart%
.This returned row having 'liver heart'
but not those rows that have heart at the beginning of string. I am kind of stuck here..so guys help me.
编辑在您的帮助下,我提供了以下解决方案,但仍然给我带来了一些问题.
EDITWith your help guys i came with the following solution but still its giving me some problem.
SELECT q.question_id, q.question, q.date,p.fname,p.lname,p.phys_id,
p.pic_path
FROM questions q JOIN physiciansprofile p ON p.phys_id=q.phys_id
WHERE q.question LIKE 'heart%' OR question LIKE '% heart%'
AND q.question LIKE 'liver%' OR q.question LIKE '% liver%'
ORDER BY q.date DESC LIMIT 0,10;
但是此查询也会返回heart failure
和symptoms liver
,对此是否有解决方案我需要获取同时包含heart
和liver
的结果,并且还必须满足前面所述的条件.有什么要解决的吗
But this query return heart failure
and symptoms liver
as well.Is there any solution for this. I need to get result containing both heart
and liver
and also must satisfy condition as stated before. Is there any to solve this
推荐答案
'%heart%'无效,因为您要的是任何东西,再加上空间,再加上心脏,再加上任何东西.
'% heart%' doesn't work because you are asking for anything, plus space, plus heart, plus anything.
尝试类似的方法:
like 'heart%' OR like '% heart%'
这篇关于像搜索一样在mysql中包含空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!