저장 프로 시저가 있습니다.
ALTER PROCEDURE [dbo].[p_GetOrdersList]
@OrderID int = NULL,
@UserID int = NULL,
@IsCompleted smallint = NULL
AS
BEGIN
select
tto.OrderID, tto.DateAdded, tto.ItemsCount, tto.TotalAmount,
ttu.FirstName, ttu.LastName, ttu.Email,
ISNULL(ttu.FirstName,'') + ' ' + ISNULL(ttu.LastName,'') FullUserName,
tuq.QuoteStatusID
from
tbl_Orders tto
join
tbl_Users ttu on tto.UserID = ttu.UserID
where
tto.OrderID = ISNULL(@OrderID,tto.OrderID)
AND tto.UserID = ISNULL(@TronixUserID,tto.TronixUserID)
AND EXISTS (SELECT *
FROM tbl_UserQuotes ttuq
WHERE ttuq.OrderID = tto.OrderID
AND ttuq.QuoteStatusID = ISNULL(@IsCompleted, ttuq.QuoteStatusID)
)
--QuoteStatusID: 4.Completed other than 4 are Pending/Incomplete/Processing
END
위의 쿼리에서 테이블의 tbl_Orders
모든 행에 ie tbl_UserQuotes
가 있는 경우에만 테이블에서 모든 레코드를 가져오고 싶습니다 . 단일 행이 4가 아닌 경우 목록에 나타나지 않아야합니다.QuoteStatusID = 4
Completed
MasterTable(tbl_Orders)
OrderID ITemsCount TotalAmount
1 1 100
2 2 200
3 2 300
DetailTable(tbl_UserQuotes)
DetailID OrderID Amount QuoteStatusID
1 1 100 4
2 2 100 2
3 2 100 4
4 3 150 4
5 3 150 4
Result Must be:
OrderID TotalAmount
1 100
3 300
내 솔루션은 다음과 같습니다.
Select tto.OrderID,tto.DateAdded,tto.ItemsCount,tto.TotalAmount,
ttu.FirstName,ttu.LastName,ttu.Email ,ISNULL(ttu.FirstName,'') + ' ' + ISNULL(ttu.LastName,'') FullUserName
from tbl_Orders tto join tbl_Users ttu
on tto.UserID = ttu.UserID
where tto.OrderID = ISNULL(@OrderID,tto.OrderID)
AND tto.UserID = ISNULL(@UserID,tto.UserID)
AND tto.ItemsCount= (
select count(orderid) from tbl_UerQuotes where orderid=tto.OrderID and quotestatusid=4
)
ORDER BY tto.DateAdded DESC
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다