在列perticulat类别vise MySQL上查找具有相同值的行

时间:2016-07-16 05:57:40

标签: php mysql database

table : tbl_subcategory

我想获取where子句中cat_id 2和4的相同SNAME。 并且在cat_id 1,2,4中,类别1,2,4的SNAME DB字段中没有相同的记录,因此没有找到该记录 - 但在cat_id 2和4配件或少数记录是相同的。所以它会显示在结果中。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以尝试这种方式,在同一个表中使用内连接

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;

我使用内部联接来匹配具有相同sname ..i的行,在分隔别名表中选择与cat_id匹配的行..所以在您的情况下我使用两个别名表(a和b)因为你正在寻找2 cat_id(2和4)..如果你需要3例如2,3,4 rhe查询是

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;