5

How can I query data from a table available on a list of strings?

I only want to get the data from the list of strings.

Example:
Table
ID Name
1  Big
2  Small
3  Extra
4  Orange
5  Drink

List of Strings:
Big
Small
Extra

Thanks!

2
  • Where does your list of strings come from? What client language are you using? What is the maximum number of items you expect that there could be in the list? Commented Apr 27, 2010 at 19:47
  • The list of strings came from a query. I'm using PHP. The maximum data would be at least 50 Commented Apr 28, 2010 at 17:00

4 Answers 4

12

I assume you want the MySQL IN clause:

SELECT ID, Name FROM TableName WHERE Name IN ('Big','Small','Extra')
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, what will MySQL do if you try to name a table table?
+1 even though you were 9 seconds slower... for using an explicit list of columns in the select clause.
FWIW, I started with an explicit list, but figured the OP must have other columns he wasn't showing us, and that he needed all the help he could get...
3
SELECT * FROM theTable WHERE column_name IN ('Big', 'Small', 'Extra')

Comments

1

I had to stringify any numbers if I was looking for a MIX of numbers and strings, curiously it was the strings I couldn't select if I didn't quote the numbers.

SELECT * FROM theTable WHERE column_name IN ('0','','23','Big', 'Small', 'Extra')

Comments

0

One more variant if you have list of values in variable:

SET @list = 'Big,Small,Extra';

SELECT * FROM theTable WHERE FIND_IN_SET(name, @list);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.