在下面的代碼中,如果@selectAll = 1,我不希望連接條件的第二部分被適用。 (即和(listMap.dateOfTask在@startDate和@endDate之間) 我使用的是SQL Server.
。@userId nvarchar(200) 。
@startDate nvarchar(200)。
@endDate nvarchar(200)。
@selectAll bit
select[/span]。
*位
from tmListMap l
right join tmTime t
on
l.timeId = t.timeId
and (l.dateOfTask between @startDate and @endDate) //不需要這個條件。 關鍵字">條件如果@selectAll = 1
uj5u.com熱心網友回復:
@Damien建議你需要一個or條件,如
@userId nvarchar(200) 。
@startDate nvarchar(200)。
@endDate nvarchar(200)。
@selectAll bit
select[/span]。
*位
from tmListMap l
right join tmTime t
on
l.timeId = t.timeId
and ((l.dateOfTask between @startDate and @endDate) or @selectAll=1)
uj5u.com熱心網友回復:
@userId nvarchar(200) 。
@startDate nvarchar(200)。
@endDate nvarchar(200)。
@selectAll bit
select[/span]。
*位
from tmListMap l
right join tmTime t
on
l.timeId = t.timeId
where @selectAll = 1 or (l. dateOfTask between @startDate and @endDate)
uj5u.com熱心網友回復:
兩個選項。
選項A為OR子句
@startDate date,
@endDate date,
@selectAll bit
select
*位
from tmListMap l
right join tmTime t
on l.timeId = t.timeId
WHERE
(
l. dateOfTask between @startDate and @endDate and @selectAll = 0
OR OR
@selectAll = 1
)
選項B為UNION ALL
@startDate date,
@endDate date,
@selectAll bit
select
*位
from tmListMap l
right join tmTime t
on l.timeId = t.timeId
WHERE @selectAll = 0
and l.dateOfTask between @startDate and @endDate >。
UNION ALL
select[/span
*
from tmListMap l
right join tmTime t
on l.timeId = t.timeId
WHERE @selectAll = 1
這有點取決于你的資料,哪個會更快。根據我的經驗,UNION ALL的擴展性更好。只要不同的部分絕對不能創建重復的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311089.html
標籤:
