Home > Software design >  Searching for multiple keywords in a string
Searching for multiple keywords in a string

Time:05-17

So I'm trying to perform an SQL query to find multiple "text1", "text2" in a single string in a particular column and was wondering if someone could help.

Example: Query for: Blue Beige Column "Blue and Beige t-shirt" "Red and Blue t-shirt" "Red t-shirt" "Blue t-shirt"

Result should be ONLY "Blue and Beige t-shirt" as it meets the criteria of having for query. Like "%" functions seem to not be useful.

Another example would be query for: "iPhone 256GB" Column "iPhone Red 128GB" "iPhone 256GB" "iPhone Pro Max 256GB" "iPhone se 128GB"

Results should be: "iPhone 256GB" "iPhone Pro Max 256GB"

please help

CodePudding user response:

If you are looking for entries that contain each space-separated input regardless of order, so for instance if your search text is "iphone 256" and you want to find any entries that contain both "iphone" and "256" regardless of order, then you would do:

SELECT * FROM <table> WHERE <column> LIKE '%iphone%' and <column> LIKE '%6%'

If you want the results to only include entries where the keywords are in the given order, you can do:

SELECT * FROM <table> WHERE <column> LIKE '%iphone%6'

CodePudding user response:

To build on vball's answer for your Blue Beige search. You could use the same technique, which does include the % wildcard. If the colors can come in any order:

SELECT * FROM <table> WHERE <column> LIKE '%Blue%' AND <column> LIKE '           
  • Related