1

So, I've been doing some digging but I can't really find something specific. Let's say I have a table called products with 2 columns: product_name and vendor.

product_name  |  vendor
Item A        | Computer Mania
Item B        | Incredible Connection
Item C        | Computer Mania

So, now let's say hypothetically I don't remember how to spell computer and I say, okay I remember the name starts with co and ends with er and there is mania afterwards.

How can I build a SELECT query with those conditions in my WHERE? I have no idea how to do it.

Here's what I tried: SELECT * FROM products WHERE vendor_name LIKE '%co%';

However this returns Incredible Connection as well, which I don't want.

3 Answers 3

1

I would use REGEXP here:

SELECT *
FROM yourTable
WNERE vendor REGEXP '^co[a-z]*er mania';

This regex pattern says to find all vendors whose names have a first word starting with co and ending in er, followed by the word mania.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to use SQL Wildcards properly.

The % wildcard represents zero or more characters. So, if you use %co%, it will search the string co in the vendor column. vendor column in given example in the question has the string co in every row.

If you say that it starts with co and ends with er mania, then use the wildcards as vendor like 'co%er mania'. This will search the vendor column for the values which starts with co and ends with er mania

select * from products where vendor like 'co%er mania';

Refer this to read more about SQL Wildcards.

Comments

0

For your example, you would use like:

where vendor_name like 'co%er mania'

You may want to make this case independent as well, although that is the default in MySQL.

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.