我是新手,所以如果我没有正确的短语来表达所有内容,请耐心等待。 ;-)
我正在尝试查询数据库。我有一个可以有 1-3 个固定值的字符串,这些值来自复选框输入表单的内爆 $_Post["array"] 。值类似于:
$string // values (Berlin, New York, Rome) or (Berlin, Rome) ect.
现在我正在尝试查询一个数据库,其中每行(项目)都有一个列(字段),其中包含 1-3 个相同的值。
item1 (New York, Rome)
item2 (Berlin, New York, Rome)
item3 (Rome)
我尝试过这样的查询:
SELECT * FROM 'table' WHERE 'destination' LIKE '%$string%'
or
SELECT * FROM 'table' WHERE 'destination' IN '$string'
虽然理论上两者都有效,但它们并没有给我带来我想要的结果。这是我希望从查询中得到的内容:
If $string has all 3 values, I want to display all 3 items (not only item2).
If $string has value(Rome), I want to display item1, item2 and item3.
If the $string has the value(New York, Berlin) I want to display item1 and item2
有什么想法可以实现这一目标吗?
非常感谢 塞布
最佳答案
假设您的意思是您的表列名称是目标并且 $string 是表单中的后值,您可以尝试 FIND_IN_SET :
SELECT * FROM `table` WHERE FIND_IN_SET($string, `destination` );
关于PHP MySQL 查询数据库的字符串/数组结果问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274918/