php SQL 喜欢和喜欢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9072782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Like and like
提问by RobM
Is it possible to string together multiple SQL LIKE wildcards in one query - something like this?
是否可以在一个查询中将多个 SQL LIKE 通配符串在一起 - 像这样?
LIKE '% aaaa %' AND LIKE '% bbbb %'
The aim is to find records that contain both wild cards but in no specific order.
目的是查找包含通配符但没有特定顺序的记录。
回答by Vyktor
The correct SQL syntax is:
正确的 SQL 语法是:
field LIKE '% aaaa %' AND field LIKE '% bbbb %'
回答by Sparky
Yes, that will work, but the syntax is:
是的,这会起作用,但语法是:
Field LIKE '%aaa%' AND field LIKE '%bbb%'
回答by John Woo
This is useful when you are using this statement with variables.
当您将此语句与变量一起使用时,这很有用。
SELECT *
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', 'aaaa', '%') AND -- if aaaa is direct Text
`colName` LIKE CONCAT('%', 'bbbb', '%')
SELECT *
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', aaaa, '%') AND -- if aaaa is variable
`colName` LIKE CONCAT('%', bbbb, '%')
回答by nickf
Yes, but remember that LIKE is an operator similar to ==
or >
in other languages. You still have to specify the other side of the equation:
是的,但请记住,LIKE 是与其他语言类似==
或>
在其他语言中使用的运算符。您仍然必须指定等式的另一侧:
SELECT * FROM myTable
WHERE myField LIKE '%aaaa%' AND myField LIKE '%bbbb%'
回答by Justin ??????
It is possible to string together an arbitrary number of conditions. However, it's prudent to use parenthesis to group the clauses to remove any ambiguity:
可以将任意数量的条件串在一起。但是,谨慎的做法是使用括号将子句分组以消除任何歧义:
SELECT *
FROM tableName
WHERE (columnOne LIKE '%pattern%')
AND (columnTwo LIKE '%other%')