Reputation: 2187
in a string array i have a variable amount of values. how will an sql statement look if i want to select all the records that are equal with the array variables.
it will look something this:
SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"
Upvotes: 0
Views: 200
Reputation: 15085
Another approach, although less efficient, can be used if parsing the string apart is problematic...
DECLARE @listOfNames VARCHAR(2000)
SET @ListOfNames = 'JOE,KELLIE,PETE'
SELECT * FROM users WHERE charindex(","+userName+",",","+@listOfNames+",") > 0
This approach is slower than either of the other answers, but can save you from using dynamic SQL to build a SQL query based on the list of names passed in...
Upvotes: 0
Reputation: 3682
Every one above me is corrrect! There are multiple ways of doing this! A simple google search for 'mysql statement variables' yield tons of help full results including:
http://www.daniweb.com/forums/thread126895.html
Just treat your array like a standard varible with [a number] on the end!
Two tips come from this:
Google / Search your problem first 9 times out of 10 someone else has had the same problem and found a working solution!
And be prepared to look at the answers on here with in 5 mins. This forums probably the quickest you'll ever see. The only thing that slows the community down is typing ! :P
Hope that Helps,
Andy
Upvotes: 0
Reputation: 3461
You might be looking for the IN( ) operator.
SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');
Upvotes: 6
Reputation: 14321
This? (obviously the "OR username='xxx' is repeated for as many items as you require)
SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'
Upvotes: 1