Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

php - MySQL查询字符串包含(MySQL query String contains)

I've been trying to figure out how I can make a query with MySQL that checks if the value (string $haystack ) in a certain column contains certain data (string $needle ), like this:

(我一直在尝试找出如何使用MySQL进行查询,以检查某个列中的值(字符串$haystack )是否包含某些数据(字符串$needle ),如下所示:)

mysql_query("
SELECT *
FROM `table`
WHERE `column`.contains('{$needle}')
");

In PHP, the function is called substr($haystack, $needle) , so maybe:

(在PHP中,该函数称为substr($haystack, $needle) ,所以也许:)

WHERE substr(`column`, '{$needle}')=1
  ask by arik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quite simple actually:

(实际上很简单:)

mysql_query("
SELECT *
FROM `table`
WHERE `column` LIKE '%{$needle}%'
");

The % is a wildcard for any character.

(%是任何字符的通配符。)

Do note that this can get slow on very large datasets so if your database grows you'll need to use fulltext indices.

(请注意,这对于非常大的数据集可能会变慢,因此,如果数据库增长,则需要使用全文索引。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...