Home > Enterprise >  Search for more than one column in the same query
Search for more than one column in the same query

Time:12-15

So I have a task to basically just by typing the first letter of the name or type on a table(product), show all the names that start with that letter or types that start with that letter. Now the problem is: i dont know how to do it for the two columns(name, type) basically all i have is this but i need to do the same for type in the same query. how tho?

SELECT id, type, name,price FROM product where name like 'cok%'

CodePudding user response:

If I understood your question correctly, you are looking for a simple OR condition in your query:

SELECT id, type, name, price
FROM product
WHERE name ILIKE 'cok%' OR type ILIKE 'cok%'

Note I changed it from LIKE to ILIKE to make it case-insensitive, which seems to be more sensible in your case.

CodePudding user response:

You probably need two queries if you want to show types that start with something and names that start with something. Or, if you want it combined, as in show all products where the name or the type starts with the letter, use OR:

SELECT id, type, name,price FROM product 
 WHERE name like 'cok%'
    OR type like 'cok%'
  • Related