如何在這樣的查詢中使用?有沒有辦法在TableList中使用tableIds?我可以把它們當作子查詢嗎?在where子句中使用sql與
select * from TestTable where tableId in
(WITH TableList (tableId)
AS
(
SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
UNION ALL
SELECT t.tableId From OldTable as t
INNER JOIN TableList as p
ON t.ParenTableId = p.TableId
WHERE t.IsDeleted = 0
)
SELECT tableId FROM TableList
)
編輯:爲了說明爲什麼我不能在sql的第一條語句中使用。
select TableId, IsDeleted,
(
select count(OtherId) OtherTable
where otherTableId in
(WITH TableList (tableId)
AS
(
SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
UNION ALL
SELECT t.tableId From OldTable as t
INNER JOIN TableList as p
ON t.ParenTableId = p.TableId
WHERE t.IsDeleted = 0
)
SELECT tableId FROM TableList
)
) as Ex1
from TestTable where tableId in (1,2,3)
我覺得這個節目爲什麼我嘗試在SQL查詢的中間有子句中使用。
你想在這裏完成什麼? –
即時嘗試讓表遞歸。在我的OldTable列中,像TableId,ParentTableId和Isdeleted。通過使用這個SQL即時通訊試圖獲取tableId = 5 –