我试图得到一个结果,其中单个列 id 就像逗号分隔的列,我的两个表如下:
房间位置表
事件表
用户表
我的查询是
$sql=" SELECT users.*, events.*, room_location.*
FROM events
INNER JOIN room_location ON events.event_room = room_location.location
INNER JOIN users ON room_location.user_loc_id = users.userlocationaccess
WHERE room_location.user_loc_id LIKE '%1,2%'";
$result = $conn->query($sql);
如果我在 users->userGroupLocID 中使用单个 ID,则上面的查询有效。
如何修改查询以使其正常工作,以便查找 id 是否类似于我的逗号分隔列?
最佳答案
您可以使用 FIND_IN_SET(str,strlist)
搜索 CSV 字段功能。
示例:
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2
文档说:
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters.
您可以传递用户位置ID以在userGroupLocID
列中查找
select find_in_set( location_id_in_search, replace( userGroupLocID, ' ', '' ) )
from table_name;
关于php - 使用单个 id 列查询逗号分隔列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37744491/