【问题标题】:MySQL Select where column equals part of stringMySQL选择其中列等于字符串的一部分
【发布时间】:2018-11-04 14:16:38
【问题描述】:

我想知道是否有这样的查询:

表格列值:

harken op het strand

输入字符串:

Jantje ging harken op het strand omdat hij daar zin in had.

结果(1 行):id "1", value "harken op het strand";

输入字符串:

Jantje ging met harken het strand op omdat hij daar zin in had.

结果:空。

我知道它必须是这样的:

SELECT *
FROM table
WHERE value LIKE 'Jantje ging harken op het strand omdat hij daar zin in had.'

LIKE 试图将字符串的每个部分与数据库表的列行匹配。

【问题讨论】:

    标签: php mysql sql phpmyadmin


    【解决方案1】:

    对于 Gordon 的回答稍作修改,我们可以尝试在此处使用 REGEXP 并带有适当的字边界:

    SELECT t.*
    FROM yourTable t
    WHERE
        'Jantje ging harken op het strand omdat hij daar zin in had.'
        REGEXP CONCAT('[[:<:]]', t.value, '[[:>:]]');
    

    【讨论】:

      【解决方案2】:

      我想你想要 LIKE 以其他顺序:

      SELECT t.*
      FROM table t
      WHERE 'Jantje ging harken op het strand omdat hij daar zin in had.' LIKE CONCAT('%', t.value, '%')
      

      【讨论】:

        【解决方案3】:

        另一种可能性是使用Locate() 函数。它可能比使用Like %..% 更高效。我们将简单地检查输入字符串值中字段(列)值(子字符串)的第一次出现。如果存在,我们已经找到匹配项:

        SELECT *
        FROM your_table_name 
        WHERE LOCATE(value, 'Jantje ging harken op het strand omdat hij daar zin in had.') > 0
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-26
          • 1970-01-01
          • 2018-09-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多