Anonymous
Anonymous

Reputation: 3689

check for appearance of multiple strings in mysql

I got a database with multiple fields and about 10000 entries. I only need to check for the appearance of 4 different strings in one field.

Example:

id--name--sex--color--size--weight--price--points--class--
1   pet   m    white  180   90      120    1000    low
2   bob   m    white  180   90      110    2000    high
3   foo   m    white  180   90      120    3000    low
4   boo   m    white  180   90      140    1000    low
5   bla   m    white  180   90      100    2000    medium
6   blu   m    white  180   90      120    5000    lala    

Basically there are 3 classes, but sometimes I have a custom class. Now I want to search my whole DB for the appearance of low, high and medium. It might be that there is no entry for one or more of them. Also I need to check whether there is a custom (unknown) class. It's enough to know whether they appear, I don't need any more data.

All I came up with is just querying the field class and then searching with phps in_array(), but I guess that's just a waste of resources.

Can someone help me here please ? How do I query this clever without querying all the results?

Upvotes: 0

Views: 196

Answers (2)

Jan Korous
Jan Korous

Reputation: 666

Let database find only distinct values from class column. Your keyword is 'distinct'.

select DISTINCT class from table;

Anyway if you care I would recommend reading something about database table normalization and improving the table design.

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270599

Can you not simply query for counts, GROUP BY class? This will return all classes, including your custom ones.

SELECT class, COUNT(*) FROM table GROUP BY class;

Or simply DISTINCT class

SELECT DISTINCT class FROM table

Upvotes: 1

Related Questions