gpt4 book ai didi

php - 如何查找给定句子中存在的列值? mysql

转载 作者:行者123 更新时间:2023-11-29 11:18:47 26 4
gpt4 key购买 nike

考虑下面的 mysql 表

id         name
----- ------
1 Mark
2 Mike
3 Paul
4 John

并考虑输入句子

Mark and Paul are very good friends since 2000.

预期输出

Mark , Paul

我想找到输入句子中出现的名字。mysql 是否提供任何选项来查找此内容。或者有什么想法吗?

最佳答案

这可以通过两种方式实现。使用mysql的'IN'和'REGEX'运算符。

通过使用“IN”运算符

<?php

$string = "Mark and Paul are very good friends since 2000.";
$sql_in = "'".implode("','",explode(" ",$string))."'";

//Will return all the records for which name match with any word in given sentence.
$mysql_query = "select * from table_name where name in($sql_in)";

?>

通过使用“REGEX”

<?php

$string = "Mark and Paul are very good friends since 2000.";
$regex_string = str_replace(' ','|',$string);

//Will return all the records for which name match with any word in given sentence.
$mysql_query = "select * from table_name where name REGEX '^(".$regex_string.")'";

?>

关于php - 如何查找给定句子中存在的列值? mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com