1

In my table a column called group_members_id contains userid of 10 users. I've been trying to figure out how I can make a query with MySQL that fetch the details of that row if given userid matches any of those 10 userid.

"SELECT * FROM `table` WHERE `group_members_id`.contains('{$userid}')".

This code is not working. can anyone help me?

Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
Anweshan Paria
  • 21
  • 1
  • 1
  • 2
  • Possible duplication http://stackoverflow.com/questions/6121630/how-to-write-mysql-query-where-a-contains-a-or-b – rokas Jul 25 '16 at 09:05
  • just to classify, you are trying to pull records from the SQL table that have the group_members_id = xyz and in the field group_members_id there is a list like {1},{2},{3},{4}...ect ect?? – Zapps Ceo Jul 25 '16 at 09:08
  • You can use find_in_set. – RN Kushwaha Jul 25 '16 at 09:17

4 Answers4

3

You can get use like this

"SELECT *
FROM `table`
WHERE `group_members_id` LIKE '%{$userid}%'"
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
2

Use like clause

"SELECT * FROM table WHERE group_members_id LIKE '%$userid%'";
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

Considering all the user IDs are of INT type:

<?php
//$aUserIds is an array of  your User Ids.
$userIds = implode(",", $aUserIds);
$sQuery  = "SELECT * FROM table WHERE group_members_id IN (" +  $uSerIds + ")";

Alternatively, if they are of string types, use:

$userIds = implode(",", $aUserIds);
$sUserIds = "";
foreach($userId as $userIds)
{
   $sUserIds += "'" + userId + "'";
}
$sQuery  = "SELECT * FROM table WHERE group_members_id IN (" +  $sUserIds + ")";
pioneer
  • 103
  • 5
0

First, always use group_member_id as foregin id and make that as int.

Second, never use integer value as string. it will help in other functions like sort etc.

Third, If you have stored integer value as string in the table then you can use as follows:

1.) Use the LIKE Clause keyword, along with percent signs in the string

"SELECT * FROM table WHERE group_members_id LIKE '%$userid%'";

2.) If you're looking for more a complex combination of strings than you've specified in your example, you could regular expressions (regex): See the MySQL manual for more on how to use them: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75