I have 2 tables.
One is bom_master:
| CHILD | PARENT |
| ------------ | ------------ |
| 1-111 | 66-6666 |
| 2-222 | 77-7777 |
| 2-222 | 88-8888 |
| 3-333 | 99-9999 |
Another one is library:
FileName | Location |
---|---|
66-6666_A.step | S:\ABC |
77-7777_C~K1.step | S:\DEF |
And I want to find out if the child's parents have related files in the library.
Expected Result:
CHILD | PARENT | FileName |
---|---|---|
1-111 | 66-6666 | 66-6666_A.step |
2-222 | 77-7777 | 77-7777_C~K1.step |
Tried below lines but return no results. Any comments? Thank you.
WITH temp_parent_PN(parentPN)
AS
(
SELECT
[PARENT]
FROM [bom_master]
where [bom_master].[CHILD] in ('1-111','2-222')
)
SELECT s.[filename]
FROM [library] s
WHERE EXISTS
(
SELECT
*
FROM temp_parent_PN b
where s.[filename] LIKE '%'+b.[parentPN]+'%'
)
2条答案
按热度按时间wgxvkvu91#
If you have just one level of dependencies use the join solution proposed by dimis164.
If you have deeper levels you could use recursive queries allowed by WITH clause ( ref. WITH common_table_expression (Transact-SQL) ).
This is a sample with one more level of relation in bom_master (you could then join the result of the recursive query with library as you need).
wpcxdonn2#
I think you don't have to use exists. The problem is that you need to substring to match the join. Have a look at this: