简体   繁体   English

查找逗号分隔的关键字列表

[英]Finding a list of comma separated keywords

When I run my code I cannot retrieve the entry that I am targeting because the keywords that are being run through the query are not the same as the keywords in the database. 运行代码时,无法检索到我定位的条目,因为通过查询运行的关键字与数据库中的关键字不同。

Keywords: 关键字:

$keywords = 'spaghetti,macaroni,hamburger';

Query: 查询:

mysql_query("SELECT * FROM ---- WHERE keywords LIKE '%$keywords%' ORDER BY id DESC LIMIT 1");

I am targeting an entry with these keywords: 我的目标是使用这些关键字的条目:

food, restaurant, hamburger, spaghetti, taco,pie

I may be missing something simple here since I've been working for 12 hours straight. 因为我已经连续工作了12个小时,所以这里可能缺少一些简单的东西。 However, I've had no luck with anything I've tried to do to work around this issue. 但是,我为解决此问题所做的任何尝试都没有运气。

From my point of view the query should be returning the target entry since there are 2 matching keywords. 从我的角度来看,该查询应返回目标条目,因为有2个匹配的关键字。

What should I be doing differently? 我应该怎么做?

You need to break up the keywords and do a separate LIKE comparison for each one 你需要打破的关键字,做一个单独的LIKE为每一个比较

SELECT * FROM mytable
WHERE keywords LIKE '%spaghetti%' OR keywords LIKE '%macaroni%'
-- ...

You could do something like this 你可以做这样的事情

<?php

$keywords = 'spaghetti,macaroni,hamburger';

$query = "SELECT * FROM mytable WHERE 1=1";

$keywords = explode(',', $keywords);

if (count($keywords)) {
    $query .= sprintf(
        " AND (%s)",
        implode(' OR ', array_map(function($word){
            return "keywords LIKE '%{$word}%'";
        }, $keywords))
    );
}

echo $query;

// SELECT * FROM mytable WHERE 1=1 AND (keywords LIKE '%spaghetti%' OR keywords LIKE '%macaroni%' OR keywords LIKE '%hamburger%')

As you can see from the comments, there are better ways to handle this, but I'm assuming that this rudimentary question is looking for a simple answer. 从评论中可以看到,有更好的方法来解决此问题,但是我假设这个基本问题正在寻找一个简单的答案。

I'm not doing any input sanitization, or adding any other fail safes. 我没有进行任何输入清理,也没有添加任何其他故障保险柜。 Building queries like this is not very reliable and you should continue to learn about PHP/MySQL to avoid common pitfalls in the future. 建立这样的查询不是很可靠,您应该继续学习PHP / MySQL以避免将来遇到常见的陷阱。 However, I believe more advanced approaches are a bit out of your reach at the moment. 但是,我认为目前尚无法使用更高级的方法。 If someone else would like to provide better techniques that they think the OP can grasp, by all means, go for it :) 如果其他人想提供他们认为OP可以掌握的更好的技术,那就去吧:)

$like = array();
$keywords = explode(',' , $keywords);
foreach($keywords as $keyword) {
  $like[] = "`keywords` LIKE '%$keyword%'";
}
$like = implode(' OR ', $like);
$query = "SELECT * FROM `table` WHERE $like ORDER BY id DESC LIMIT 1";

Storing multiple values as a CSV inside a column is a common SQL anti-pattern. 在列中将多个值存储为CSV是一种常见的SQL反模式。 It's hard to use and even harder to optimize: 它很难使用,甚至难以优化:

How to fix the Comma Separated List of Doom 如何修复末尾逗号分隔列表

Try this, it may be faster than using multiple or clauses 试试这个,它可能比使用多个or子句更快

Select * from mytable
where keywords REGEXP 'spaghetti|macaroni|hamburger'

You have to use LIKE with separeted works, you can try this: 您必须将LIKE与分离的作品一起使用,可以尝试以下操作:

$keywords = 'spaghetti,macaroni,hamburger';

$arrayWords = explode(',', $keywords);

$like = '';

foreach($arrayWords as $word)
    $like .= empty($like) ? " keywords LIKE '%$word%' " : " OR keywords LIKE '%$word%' ";

mysql_query("SELECT * FROM ---- WHERE $like ORDER BY id DESC LIMIT 1");
mysql_query("SELECT * FROM ---- WHERE keywords LIKE '%". implode("%' OR keywords LIKE '%", explode(",", $keywords)) ."%' ORDER BY id DESC LIMIT 1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM