我有一条MySQL语句,可帮助您为推荐列表找到相似的波段。它可以满足我的要求,但是一件小事。它不会阻止已经被查询的乐队。

这是声明:

foreach ($recomBands as $val) {
    $genres[] = "'%".$val."%'";
}
$string = implode(' OR `genre` LIKE ', $genres);

foreach ($favedBands as $value) {
    $bands[] = "'" . sanitize($value) . "'";
}
$bandString = implode(' OR `band_name` != ', $bands);

$sql = "SELECT `band_name`, `band_id` FROM `song_genres` WHERE `band_name` != {$bandString} AND `genre` LIKE {$string}";

// QUERY CODE AND WHAT NOT GOES HERE. ALL WORKING FINE


呼应:

 SELECT `band_name`, `band_id` FROM `song_genres` WHERE `band_name` != 'Bears and Company' AND `genre` LIKE '%Indie Rock%' OR `genre` LIKE '%Progressive Rock%' OR `genre` LIKE '%Alternative Rock%' OR `genre` LIKE '%Indie Folk%'


这是一个有效的查询(尽管不是大多数人会做的确切的事情,但这很重要)。输出数组显示

array(17) {
    [0]=>
    string(20) "Henry's Funeral Shoe"
    [1]=>
    string(17) "Bears and Company"
    [2]=>
    string(17) "Bears and Company"
// AND A LIKE 15 MORE OF BEARS AND COMPANY


目的是比较一个乐队中找到的所有流派,并将它们与其他具有类似流派的乐队分组,然后对其进行排序。一切正常,但是我想从查询中看到的范围仍然显示。为什么?

附言我还有其他虚拟帐户,可以按随机顺序测试多个乐队,流派等。

最佳答案

在SQL中不等于的是<>

因此更改为

$bandString = implode(' OR `band_name` <> ', $bands);


而且,您还必须对()陈述表白

SELECT `band_name`, `band_id`
FROM `song_genres`
WHERE `band_name` != 'Bears and Company' AND (`genre` LIKE '%Indie Rock%' OR `genre` LIKE '%Progressive Rock%' OR `genre` LIKE '%Alternative Rock%' OR `genre` LIKE '%Indie Folk%')


请改用准备好的语句。它更具安全性和可读性。

09-25 14:15