0

I have a table that looks like the following:

ID   Customer Name   LinkedID
1    A        X      
2    B        Y      1
3    C        Z      2

I need to write a query that will:

1- For each ID (1,2,3)
2- If there is a LINKEDID defined (2,3 have links)
3- If the CUSTOMER of the LINKEDID is A
4- Return record for that ID

So in the above example it would do something like:

  • ID's 2,3 have LinkedID's specified
  • ID2 links to #1 which is customer A (good) return ID2
  • ID3 links to #2 which is customer B (bad)

So the end result would be ID2.

SELECT * FROM TABLE WHERE LINKED <> NULL    // this will get me the list
? But now how do I iterate over each of the results and compare the customers?

Any help would be much appreciated. Thanks,

4

2 回答 2

1
select t1.id
from your_table t1
inner join your_table t2 on t1.linkedid = t2.id
where t2.customer = 'A'
于 2013-11-12T19:31:15.470 回答
1

加入表两次。C是主表,P代表linkedID引用的父表。

SELECT  *
FROM    CustomerTable C
JOIN    CustomerTable P ON P.ID = C.LinkedID
WHERE   P.Customer = 'A'
于 2013-11-12T19:32:08.583 回答