Php 如何使用WHERE条件从表中的字符串中查找特定值?

Php 如何使用WHERE条件从表中的字符串中查找特定值?,php,mysql,regex,Php,Mysql,Regex,需要: id | city | cusine_type ------------------------------------ 1 | Coimbatore | 3 Star Hotel 2 | Coimbatore | 5 Star Hotel, Bakery 3 | Coimbatore | 3 Star Hotel, Ice Cream Store 4 | Coimbatore | Star Hotel, Restaurant

需要:

id   |   city      |  cusine_type
------------------------------------
1    | Coimbatore  |  3 Star Hotel
2    | Coimbatore  |  5 Star Hotel, Bakery
3    | Coimbatore  |  3 Star Hotel, Ice Cream Store
4    | Coimbatore  |  Star Hotel, Restaurant
我需要使用mysql在字段中选择extract value。该值用逗号分隔

MYSQL表:

id   |   city      |  cusine_type
------------------------------------
1    | Coimbatore  |  3 Star Hotel
2    | Coimbatore  |  5 Star Hotel, Bakery
3    | Coimbatore  |  3 Star Hotel, Ice Cream Store
4    | Coimbatore  |  Star Hotel, Restaurant
我的PHP和MYSQL代码

include "db.php";
$city='Coimbatore';
$cus_type='Star Hotel';
$qy=mysqli_query($con, "SELECT * FROM add_res where city='$city' and cusine_type REGEXP CONCAT(',?[$cus_type],?') ");       
问题:

id   |   city      |  cusine_type
------------------------------------
1    | Coimbatore  |  3 Star Hotel
2    | Coimbatore  |  5 Star Hotel, Bakery
3    | Coimbatore  |  3 Star Hotel, Ice Cream Store
4    | Coimbatore  |  Star Hotel, Restaurant
上面的代码选择表中的所有行。我只需要第4行。

您可以这样做

cusine_type LIKE '%$cus_type%'
根据已编辑的问题进行编辑。一个好的选择是

FIND_IN_SET('$cus_type',cusine_type)


对不起,朋友。此代码不起作用。使用此代码时,不会获取任何行,但几分钟前您说它工作得很好。错误是什么?当添加一个新的cusine类型“开始酒店”时,它不起作用。它可以获取所有的行。你能给出下面的解决方案吗<代码>$cus\u type=$\u POST['cus\u type'];如果($cus_type=='all'){$cus_type='*';}其他{$cus_type=$\u POST['cus_type']}包括“db.php”$ff=mysqli_query($con,“从add_res中选择*,其中cusine_type=$cus_type”)回显构造的语句;你可能会看到这个问题。原始查询中的一个问题是对[]在REGEXP中的含义的误解。