0

我有两个如下表:

TableA --> categoryId(pk), categoryParentId, catName
TableB --> empId(pk), categoryId, empName, empDesignation

在此处输入图像描述

我想从其中of等于of中获取catName具有相应 categoryId 的所有内容。请帮忙。TableAcategoryId=2TableBcategoryParentId=2TableA

结果:

1002   SE
1003   MD
4

5 回答 5

0

我希望这个查询能满足您的需求

    SELECT TABLEA.CATNAME AS CATEGORY
    from TABLEB INNER JOIN TABLEA 
    ON TABLEA.CATEGORYPARENTID = TABLEB.CATEGORYID
于 2012-08-30T20:54:24.060 回答
0

Something like this?

SELECT DISTINCT catName FROM TABLEA WHERE categoryParentId IN (SELECT DISTINCT categoryID from TABLEB);
于 2012-08-30T20:45:19.620 回答
0

解决方案INNER JOIN

select TableA.catName
from TableA
inner join TableB on TableA.categoryParentId = TableB.categoryId

UsingINNER JOIN是用于连接表的推荐方法(而不是SELECT ... FROM TableA, TableB ...

看:


回答您的评论(我假设您想要categoryIdfrom TableA):

select TableA.catName, TableA.categoryId
from TableA
inner join TableB on TableA.categoryParentId = TableB.categoryId
where TableB.categoryId = 2
于 2012-08-30T20:49:32.650 回答
0

另一种方法...

SELECT 
  catname
from
  tablea a INNER JOIN
  tableb b ON 
    a.categoryParentID = b.categoryID
  group by
  catname
于 2012-08-30T21:04:18.817 回答
-1

This should do the trick

SELECT tableA.catName
 FROM tableA, tableB
WHERE tableB.categoryId = table1.categoryParentId
  AND tableB.categoryId = 2;

If you want the unique catNames, you can use

SELECT distinct tableA.catName
 FROM tableA, tableB
WHERE tableB.categoryId = table1.categoryParentId
  AND tableB.categoryId = 2;
于 2012-08-30T20:44:51.557 回答