【问题标题】:How to use SOUNDS LIKE in where clause如何在 where 子句中使用 SOUNDS LIKE
【发布时间】:2018-05-11 07:30:41
【问题描述】:

Codeigniter 活动记录查询给了我错误。如何将 SOUNDS LIKE 放入 where 子句。

function _search($type, $q, $qes, $sort = null, $start = 0) {
    $type = strtolower($type);
    $q = strtolower($q);


    $this->db->select("*");
    $this->db->from("books");
    $this->db->where("SOUNDEX(name) IN({$q})");
    foreach($qes as $k){
        $this->db->or_where("name SOUNDS LIKE '$k'");
    }
    foreach($qes as $k){
        $this->db->or_where("name LIKE '%$k%'");
    }
    $this->db->where("status", 1);
    if ($type != NULL) {
        $this->db->where("LOWER(type)", $type);
    }
    //$this->db->like("LOWER(name)", $q);

    $this->db->limit(BWK_MAX_BOOK_SIZE, $start);
    switch ($sort) {
        case 1:
            break;
        case 2:
            $this->db->order_by("sellingPrice", "ASC");
            break;
        case 3:
            $this->db->order_by("sellingPrice", "DESC");
            break;
        case 4:
            $this->db->order_by("created", "DESC");
            break;
  }

当我回显查询时,这给了我查询。我正在搜索 technologi 我需要获得技术技术等。

SELECT * FROM `books` WHERE SOUNDEX(name) IN('t254') OR `name` `SOUNDS` LIKE 'technolog' OR `name` LIKE '%technologi%' AND `status` = 1 AND LOWER(type) = 'school' LIMIT 50

遇到错误

  You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '`SOUNDS` LIKE 'technolog' OR `name` LIKE '%technolog%' AND `status` = 1 AND LOWE' at line 4

一切正常,但是当我输入 SOUNDS LIKE 时,它给了我错误。

【问题讨论】:

  • 从 SOUNDS 中删除反引号
  • @Bart 我如何删除。我没有放反引号它会自动使用。

标签: mysql sql codeigniter activerecord


【解决方案1】:

这里可能是最好的分组方式

你可以试试下面的

$this->db
    ->select('*')
    ->from('books')
    ->where_in('SOUNDEX(name)', $q,  NULL);

if (is_array($qes) && count($qes) > 0)
{
    $this->db->group_start();
    foreach($qes AS $k)
    {
        $this->db->or_group_start();
            $this->db
                ->where('name SOUNDS LIKE '.$this->db->escape($k), NULL, false)
                ->or_like('name', $k);

        $this->db->group_end();
    }
    $this->db->group_end();
}

if (!is_null($type))
{
    $this->db->where('LOWER(type)', $type);
}

switch ($sort) {
    case 1:
        break;
    case 2:
        $this->db->order_by("sellingPrice", "ASC");
        break;
    case 3:
        $this->db->order_by("sellingPrice", "DESC");
        break;
    case 4:
        $this->db->order_by("created", "DESC");
        break;
}

echo $this->db
    ->where('status',1)
    ->limit(BWK_MAX_BOOK_SIZE, $start)
    ->get_compiled_select();

这会产生类似的语句

SELECT *
FROM `books`
WHERE SOUNDEX(name) IN('t254') AND
(
    (
        name SOUNDS LIKE 'technologi' OR 
        `name` LIKE '%technologi%' ESCAPE '!'
    ) 
    OR 
    (
        name SOUNDS LIKE 'whatever' OR 
        `name` LIKE '%whatever%' ESCAPE '!'
    )
)
AND `status` = 1
AND LOWER(type) = 'school' 
LIMIT 50

【讨论】:

  • 它给了我 [] 结果,但在我的数据库中有名称技术、技术等字词
  • 也许里面有技术的词 - 但它们也有 soundex(name) T254 吗? - 你应该给我们一张包含受影响行的数据库快照 - 因为看起来你甚至不知道你想要什么;)
【解决方案2】:

来自:DB_query_builder.php,可以看到第三个参数决定是否逃跑。

public function or_where($key, $value = NULL, $escape = NULL){

}

所以告诉它不要逃避它

$this->db->or_where("name SOUNDS LIKE '$k'", NULL, FALSE);

【讨论】:

  • 错误消失但无法搜索到正确的结果。像技术,技术等
  • 将 where_in 与第三个参数一起使用或在 where 子句中使用第三个参数
【解决方案3】:

你能在你的 ORDER BY 和你的 LIKE 中使用 HAVING 子句吗?

HAVING 子句: https://www.w3schools.com/sql/sql_having.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2021-03-07
    • 2019-06-02
    • 1970-01-01
    • 2022-01-08
    • 2013-06-30
    相关资源
    最近更新 更多