SELECT if record CONTAINS this word?

Hi,

If the array includes these inputs in the records:

Brown bear
Brown cat
Tin can
Empty can

is there a way to SELECT results which CONTAIN words like “brown” and “can”?

I would like to have an easy way of getting information from a database without having to type in the identical match record. Say a database of recording artists records and you only want results for Rolling Stones and Kylie Minogue of which there would be 100’s of songs. You could type in SELECT which contains rolling stones and kylie minogue???

Is this possible?

Thanks,

Matt.

Try something like this:


SELECT * FROM your_table WHERE column_name LIKE '%Brown%' or column_name LIKE '%can%'

This is assuming that you are selecting from sql database, if you have XML or an array of strings you probably want to use regular expression instead(which is out of my scope of knowledge in terms of syntax). You need to replace your_table and column_name by the appropriate table and column name in your application though.

SELECT record FROM database WHERE artist = 'Rolling stones' AND artist ='kylie minogue'

Of course you are using pdo or mysqli now and not MySQL;)

The artists would be in variables and not plain text.

Thanks Hall of Famer.

If I am SELECTing from the same column do need to write “or column_name LIKE” every time or does this work too?:

SELECT * FROM your_table WHERE column_name LIKE '%Brown%', %can%'

Yeah I think so, its not too much typing though unless you are trying to match more than 3 values at the same time. You can always give a try on using comma delimiter, cant guarantee you get the same result but if that works for you please let me know.

I would be using it to list 10’s of things, maybe 100’s. Have to see how I get on.

Thanks,

Matt.