Vijay Chauhan
Vijay Chauhan

Reputation: 189

Find rows that have the same value on a column perticulat category vise MySQL

table : tbl_subcategory

i want to fetch same SNAME in which cat_id 2 and 4 in where clause. and in cat_id 1,2,4 there are no same record in SNAME DB field for category 1,2,4 so there is no record found for that - but in cat_id 2 and 4 ACCESSORIES or few record are same. so it will display in result.

how can i do that ?

Upvotes: 1

Views: 59

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You can try this way with a inner join n the same table

select * from my_table as a
inner join my_table as b on a.sname = b.sname
where a.cat_id = 2 
and b.cat_id = 4;

I use the inner join for match the row with same sname ..i select in separated alias table the rows that match a cat_id .. so in your case i use two alias table (a and b) because you are looking for 2 cat_id (2 and 4) .. if you need 3 eg 2,3,4 rhe the query is

select * from my_table as a
inner join my_table as b on a.sname = b.sname
inner join my_table as c on a.sname = c.sname
where a.cat_id = 2
and b.cat_id = 4
and c.cat_id = 3;

Upvotes: 1

Related Questions