Solved

Select value in field containing multiple values

Posted on 2006-11-12
6
2,169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…

728 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