Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql names containing 2 or more charaters

Tags:

sql

mysql

This is a question for learning sense more than I need to find a solution. I have a product entity, and it has an attribute of product_name. I would like to search product_name and return all products that have more than 1 'a' in the product_name. I need help with the query for this.

This code shows all products that have 'a'

SELECT product_name AS 'Product Names' FROM product WHERE name LIKE '%a%'

BUT I'm after products that have more than 1 'a'. Structure: Product

Product(p_Id, product_name, p_cost) 

So if I had a product called "Car Manual" it would return as there are 3 'a's. Please help.

like image 225
Jones Avatar asked Nov 28 '25 05:11

Jones


1 Answers

The query would look like:

SELECT product_name AS ProductNames
FROM product
WHERE name LIKE '%a%a%';

Don't use single quotes for column aliases. The best thing to do is to name the columns so they don't need to be escaped. Otherwise use backticks, double quotes, or square braces, depending on your database.

like image 143
Gordon Linoff Avatar answered Nov 30 '25 19:11

Gordon Linoff