SQL InnoDB为一个LIKE值检查多个列

时间:2016-10-20 20:25:51

标签: php mysql arrays myisam

我正在尝试在其中一列中找到字符串的一部分(字,字,字,字)。

我试过

SELECT * FROM table WHERE LIKE '$string%' IN (col1, col2, col3, col4, etc)

但是我得到一个致命错误,告诉我LIK附近的语法错误。

有可能这样做吗?如果是这样,我搞砸了什么?

如果我理解正确,另一种方法是爆炸字符串并做一个foreach来分别检查所有字符串?

$array = explode(',', $string);

foreach ($array as $string)
SELECT * FROM table WHERE MATCH (col1, col2, col3, col4, etc) AGAINST $string)

2 个答案:

答案 0 :(得分:0)

您可以使用或条件

对多个列执行类似操作
Sub SplitTextColumn()
'Takes all data out of one cell and splits it by line

    Dim i As Long
    Dim vA As Variant, rng As Range, c As Range
    Dim shtNew As Worksheet, sht As Worksheet

    Set sht = ActiveSheet

    Set rng = sht.Range(sht.Range("A1"), sht.Range("A1").End(xlDown))

    For Each c In rng.Cells
        vA = Split(c.Value, vbLf)
        c.Offset(0, 1).Resize(1, UBound(vA) + 1).Value = vA 
    Next

    Set shtNew = Sheets.Add(After:=sht)

    sht.Range("A1").CurrentRegion.Offset(0, 1).Copy

    shtNew.Range("a1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=True

End Sub

答案 1 :(得分:0)

你可以试试这个:

SELECT * FROM table WHERE CONCAT(col1, ' , ', col2, ' , ' col3, ' , ', col4, ' , ', etc) LIKE '$string%'