嵌套子查詢是SQL語句中比較常用的一種查詢方法,開發過程中遇到查詢需要將自己的某列作為待查詢的數據,在參考別人的SQL語句的寫法終於實現了自己需要的功能。 不太和諧查詢語句如下: SELECT DISTINCT dbo.a.ProxyID, dbo.a.account AS adminAccount ...
嵌套子查詢是SQL語句中比較常用的一種查詢方法,開發過程中遇到查詢需要將自己的某列作為待查詢的數據,在參考別人的SQL語句的寫法終於實現了自己需要的功能。
不太和諧查詢語句如下:
SELECT DISTINCT dbo.a.ProxyID, dbo.a.account AS adminAccount, dbo.b.LevelName, dbo.a.ProfitProportion, dbo.a.totalUpScore, dbo.a.score, dbo.a.members, dbo.a.authority, dbo.a.registerDate, dbo.a.freezeState, temp.belongName
FROM dbo.a INNER JOIN dbo.b ON dbo.a.ProxyLevel = dbo.b.LevelId INNER JOIN (SELECT dbo.c.BelongsAgent, a.ProxyID, a.account AS belongName FROM dbo.a AS a INNER JOIN dbo.c ON a.ProxyID = BelongsAgent AND a.ProxyID = dbo.c.BelongsAgent) AS temp ON dbo.a.belongsAgent = temp.BelongsAgent
精簡版的查詢語句如下:(省略了a表的一些不重要欄位)
SELECT DISTINCT dbo.a.BelongAgentId, dbo.a.Account, dbo.b.LevelName, temp.BelongName FROM dbo.a INNER JOIN (SELECT a.Account AS BelongName, a.ProxyId FROM dbo.a AS a INNER JOIN (SELECT BelongAgentId AS id FROM dbo.a AS a) AS t ON a.ProxyId = t.id) AS temp ON dbo.a.BelongAgentId = temp.ProxyId INNER JOIN dbo.b ON dbo.a.ProxyLevel = dbo.b.LevelId
新人報道,希望大家多多幫助,謝謝大家!