Solved

Select value in field containing multiple values

Posted on 2006-11-12
6
2,157 Views
Last Modified: 2012-05-05
I have a table (user) with a field named membergroupids which contains multiple values seperated by a comma.

The field type is varchar and will contain muiltiple member group id numbers.

for instance

Administrators group is 1
registered is 2
site users is 3
private group is 4

the field can contain this data in random entry.
one record could be   3,4
two could be             1,4
thre could be            4,3
4 could be                2,4


I am trying to figure out the sql statement that would select all records in the table that contain a 4 and ordered by userid.

examples I have tried :

SELECT * FROM user WHERE membergroupids IN (4) = 4 order by userid ASC
SELECT * FROM user WHERE membergroupids >= 4 order by userid ASC
SELECT * FROM user WHERE membergroupids IN ('4') = 4 order by userid ASC

Thank you in advance for your quick response.


0
Comment
Question by:nurv-x
  • 4
  • 2
6 Comments
 
LVL 19

Expert Comment

by:dmitryz6
ID: 17927860
use like

MS Access example

SELECT * FROM user WHERE membergroupids like *4*

sql server example

SELECT * FROM user WHERE membergroupids like %4%
0
 
LVL 19

Expert Comment

by:dmitryz6
ID: 17927864
use like

MS Access example

SELECT * FROM user WHERE membergroupids like *4* order by userid

sql server example

SELECT * FROM user WHERE membergroupids like %4% order by userid
0
 

Author Comment

by:nurv-x
ID: 17927883
That only returned records in the table that only had a 4 and nothing else in the field.

I should also mention this is php code for connection with a MySQL database.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
dmitryz6 earned 500 total points
ID: 17927895
SELECT * FROM user WHERE membergroupids like '%4%' order by userid
0
 

Author Comment

by:nurv-x
ID: 17927903
Thank you for your expert help! I was missing the ' ' around the percents!
0
 
LVL 19

Expert Comment

by:dmitryz6
ID: 17927914
You welcome and good luck with your project
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question