简体   繁体   English

查询带有单个ID列的逗号分隔列?

[英]Querying a comma separated column with a single id columns?

I am trying to get a result where a single column id is like a comma separated columns, my two table are as follows: 我试图得到一个单列ID像用逗号分隔的列的结果,我的两个表如下:

Room Location Table 房间位置表

在此处输入图片说明

Events Table 活动表

在此处输入图片说明

User Table 用户表

在此处输入图片说明

My query is 我的查询是

$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);

The query above works if I use single id in in the users->userGroupLocID. 如果我在users-> userGroupLocID中使用单个ID,则上面的查询有效。

How do I amend my query to work so it find if the id is like my comma separated columns? 如何修改查询以使其有效,以便查找ID是否类似于逗号分隔的列?

You can search a CSV field using FIND_IN_SET(str,strlist) function. 您可以使用FIND_IN_SET(str,strlist)函数搜索CSV字段。

Example : 范例

mysql> SELECT FIND_IN_SET('b','a,b,c,d');
    -> 2

Documentation Says : 文档说

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. 如果字符串str在由N个子字符串组成的字符串列表strlist中,则返回1到N范围内的值。 A string list is a string composed of substrings separated by “,” characters. 字符串列表是由用“,”字符分隔的子字符串组成的字符串。

You can pass users location id to find in the column userGroupLocID 您可以传递用户位置ID以在列userGroupLocID找到

select find_in_set( location_id_in_search, replace( userGroupLocID, ' ', '' ) )
  from table_name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM