Solved

Select value in field containing multiple values

Posted on 2006-11-12
6
2,124 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 19

Accepted Solution

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

Author Comment

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

Expert Comment

by:dmitryz6
Comment Utility
You welcome and good luck with your project
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now