开发者

simple recursive query

I have 2 tables as follows (sample data shown):

TableName: A
ID              Type
1  Bug
2  Requirement
3  Task
4  Specification
5  Bug
6  Specification
7  Production Issue
8  Production Issue
9  Bug
10          Task

Tablename: B
ID  RelatedID
1  2
1  7
5  8
5  4
9  6
9  10

I want to fetch all the bugs that have atleast one related production issue or bugs that have no related production issue.

Expected output will be as shown below (since 开发者_Python百科these are the bugs with at least one related production issue)

output
1
5


Aliases are the way to go here

SELECT pri.Type AS 'Primary Type', rel.Type AS 'Related Type' 
FROM A AS pri 
   INNER JOIN B ON B.ID = pri.ID
   INNER JOIN A AS rel ON B.RelatedID = rel.ID
WHERE pri.Type = 'Bug' AND rel.Type = 'Production Issue;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜