Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search through foreign key [closed]

Tags:

sql

php

mysql

I have one table for products in which I have many columns and I also have one column for category_id

category_id is foreign key here which refers to category table primary key

Now suppose if someone search via keyword %category_name% than I want list of product which have their category in foreign key

How to do that? Can anyone guide me on this?

like image 735
Parth shah Avatar asked Dec 28 '25 09:12

Parth shah


2 Answers

try this, it will work for you

SELECT p.*
FROM products p 
INNER JOIN category c ON p.category_id = c.category_id
WHERE c.category_name LIKE '%search%';
like image 100
Fahmi B. Avatar answered Dec 31 '25 00:12

Fahmi B.


First, you join table product to category using product.category_id as Foreign Key.

Then you filter category_name (in this case I used LIKE and keyword). And last you select columns that you need:

SELECT product.*
FROM product 
    INNER JOIN category ON product.category_id = category.category_id
WHERE category.category_name LIKE '%keyword%'
like image 25
dotnetom Avatar answered Dec 30 '25 23:12

dotnetom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!