我正在尝试根据逗号分隔字段输出选择下拉选项,例如,如果为用户分配了ID为1和2,那么我想输出位置id为1和2:
用户表: id(等于1)和usergrouplocid是一个带有(1,2)
的文本字段位置表: id为auto_increment,loc_id见下面的iamge:
我的查询如下:
$query = "SELECT room_location.*, client_room.*, users.* FROM room_location INNER JOIN client_room ON room_location.user_loc_id = client_room.id INNER JOIN users ON room_location.user_loc_id = users.userGroupLocID WHERE 1 IN (userGroupLocID) ORDER BY room_location.location";
上面的查询工作正常,但它只输出usergrouplocid中存在id为1的位置,因此如何查询查询1,2,3或4等是否在usergrouplocid中。
答案 0 :(得分:1)
万一你真的需要它现在排序而不重新设计你的数据库(我会)...
$ids = array(1,2,4);
$query = "SELECT room_location.*, client_room.*, users.* FROM room_location INNER JOIN client_room ON room_location.user_loc_id = client_room.id INNER JOIN users ON room_location.user_loc_id = users.userGroupLocID WHERE userGroupLocID REGEXP '(^|,)(".implode('|',$ids).")(,|$)' ORDER BY room_location.location";