我有一张如下表,表名是 js_skills_achievements
id skill1 skill2 skill3 skill4
1 java php html android
2 php java jquery html
3 sql php html jquery
当admin搜索键是“nersupalli中的java开发人员”
时我的查询在下面
SELECT *
FROM `js_skills_achievements`
WHERE `skill1` LIKE '%java developer in nersupalli%'
OR `skill2` LIKE '%java developer in nersupalli%'
OR `skill3` LIKE '%java developer in nersupalli%'
OR `skill4` LIKE '%java developer in nersupalli%'
ORDER BY id DESC
LIMIT 10
它不适合我。我的要求是我需要显示所有java记录。这里共有2条java记录。即id1,id2。但我得到空行。
仅限以下方式
SELECT *
FROM `js_skills_achievements`
WHERE `skill1` LIKE '%java%'
OR `skill2` LIKE '%java%'
OR `skill3` LIKE '%java%'
OR `skill4` LIKE '%java%'
ORDER BY id DESC
LIMIT 10
我需要基于字符串指定的关键字搜索。你能帮忙吗?非常感谢。谢谢
答案 0 :(得分:2)
执行此操作有点难看的方法是反转你喜欢的子句并将你的字符串与列匹配
SELECT
*
FROM
js_skills_achievements
WHERE 'java developer in nersupalli' LIKE CONCAT('%',skill1,'%')
OR 'java developer in nersupalli' LIKE CONCAT('%',skill2,'%')
OR 'java developer in nersupalli' LIKE CONCAT('%',skill3,'%')
OR 'java developer in nersupalli' LIKE CONCAT('%',skill4,'%')
OR skill1 LIKE '%java developer in nersupalli%'
OR skill2 LIKE '%java developer in nersupalli%'
OR skill3 LIKE '%java developer in nersupalli%'
OR skill4 LIKE '%java developer in nersupalli%'
ORDER BY id DESC
LIMIT 10
答案 1 :(得分:1)
在您的文件中添加全文
ALTER TABLE `js_skills_achievements` ADD FULLTEXT(`skill1`);
ALTER TABLE `js_skills_achievements` ADD FULLTEXT(`skill2`);
ALTER TABLE `js_skills_achievements` ADD FULLTEXT(`skill3`);
ALTER TABLE `js_skills_achievements` ADD FULLTEXT(`skill4`);
然后运行此查询
SELECT * FROM js_skills_achievements
WHERE MATCH(skill1,skill2,skill3,skill4) AGAINST ('java developer in nersupalli');
答案 2 :(得分:0)
您可以在空格上爆炸搜索字符串
$words = explode ( ' ' , $string);
搜索skill1中的所有单词。所有技巧2等等。
答案 3 :(得分:0)
其他答案更具技术性,可能更优化,但是如果您希望完全控制查询,例如将OR更改为AND以及类似的内容,您可以用文字拆分查询并手动构建:
//your search query
$query = 'java developer in nersupalli';
//in case you want to add more skills later
$skills = ['skill1', 'skill2', 'skill3', 'skill4'];
//split your query in an array of words
$words = explode(' ', $query);
//build the sql
$condOuter = '';
foreach($skills as $skill){
$condInner = '';
foreach($words as $word){
$condInner .= " OR `$skill` LIKE '%$word%'";
}
$condInner = substr($condInner, 4);
$condOuter .= " OR ($condInner)";
}
$condOuter = substr($condOuter, 4);
$sql = 'SELECT * FROM `js_skills_achievements` WHERE ' . $condOuter . ' ORDER BY id DESC LIMIT 10;';
答案 4 :(得分:0)
SELECT *
FROM `js_skills_achievements`
WHERE '%java developer in nersupalli%' LIKE CONCAT('%',skill1,'%')
OR '%java developer in nersupalli%' LIKE CONCAT('%',skill2,'%')
OR '%java developer in nersupalli%' LIKE CONCAT('%',skill3,'%')
OR '%java developer in nersupalli%' LIKE CONCAT('%',skill4,'%')
ORDER BY id DESC
LIMIT 10;