Search code examples
mysqlsqlselect-query

selecting rows which contain certain characters


I am working with a product database and i need to select products that are greater than £150 also only entries that have "HP" in the third and forth position of the prod_id.

So far i have tried

SELECT * FROM products WHERE prod_id LIKE 'hp%';

Here is a pic of the table i need to query

enter image description here


Solution

  • An underscore will match a single character.

    So:

    SELECT * FROM products 
    WHERE prod_id LIKE '__hp%'
    AND price > 150;
    

    Should find all products whos 3rd and 4th character are hp and have a price of 150 or more