I have these two tables and I would like to be able to search on brand.NAME and product.NAME as one string using LIKE. I have been unsuccessful using CONCAT from this in combination with JOINS so this is above my paygrade.
Is this even possible?
Wanted behavior
On search “NIKE sh”
Results:
NIKE Shoes
NIKE shirts
product
ID | NAME | brandID |
1 | Shoes | 1 |
2 | pants | 2 |
3 | shorts | 3 |
4 | shirts | 1 |
5 | socks | 2 |
brand
ID | NAME |
1 | NIKE |
2 | Adidas |
3 | Hummel |
You have to JOIN the two tables and then run the LIKE. One caveat: if you do a straight CONCAT, “Nike Shoes” will never match, because what you will have will be “NikeShoes”.
SELECT … FROM Product JOIN Brand ON (Product.BrandId = Brand.Id) WHERE CONCAT(Product.name, ‘ ‘, Brand.name) LIKE ‘%ike Sho%’;
Edit: that said, I agree with the other posters – it would be much more advantageous to look into Brands and Products separately. Unless you have a “google like” search where the user may enter whatever he fancies — but in that case I’d try to have the UI revised.