SQL 在查询中哪里使用 LIMIT
SQL where to use LIMIT in query
我试图将我的结果限制为 10。下面的查询 returns 所有结果都没有 'LIMIT 10' 但是当我添加 LIMIT 10 时它给我一个错误说
Incorrect syntax near LIMIT
我错过了什么?
SELECT dbo.Organizations.cmoName AS "Organizations not quoted"
,dbo.Calls.kbpCallID AS "kbpCallID"
,MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC LIMIT 10
在 SQL 服务器中:您可以使用前 10 个而不是限制,如下面的查询所示。
SELECT TOP 10 dbo.Organizations.cmoName AS "Organizations not quoted"
, dbo.Calls.kbpCallID AS "kbpCallID"
, MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC
在 SQL 服务器中,您按照@Zip 的建议使用 TOP。
从 SQL Server 2012 开始,您可以使用 OFFSET... FETCH
Read more on Offset... Fetch
SELECT dbo.Organizations.cmoName AS "Organizations not quoted"
,dbo.Calls.kbpCallID AS "kbpCallID"
,MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC OFFSET 0 ROWS
FETCH 10 ROWS ONLY;
如果您使用 SQL 服务器,您将使用 top(10)
而不是 Limit 10
。
我试图将我的结果限制为 10。下面的查询 returns 所有结果都没有 'LIMIT 10' 但是当我添加 LIMIT 10 时它给我一个错误说
Incorrect syntax near LIMIT
我错过了什么?
SELECT dbo.Organizations.cmoName AS "Organizations not quoted"
,dbo.Calls.kbpCallID AS "kbpCallID"
,MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC LIMIT 10
在 SQL 服务器中:您可以使用前 10 个而不是限制,如下面的查询所示。
SELECT TOP 10 dbo.Organizations.cmoName AS "Organizations not quoted"
, dbo.Calls.kbpCallID AS "kbpCallID"
, MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC
在 SQL 服务器中,您按照@Zip 的建议使用 TOP。
从 SQL Server 2012 开始,您可以使用 OFFSET... FETCH
Read more on Offset... Fetch
SELECT dbo.Organizations.cmoName AS "Organizations not quoted"
,dbo.Calls.kbpCallID AS "kbpCallID"
,MAX(dbo.CallLines.kblCallLineID) AS "kblCallLineID"
FROM (
(
(
(
dbo.Organizations LEFT OUTER JOIN dbo.Quotes ON dbo.Organizations.cmoOrganizationID = dbo.Quotes.qmpCustomerOrganizationID
) LEFT OUTER JOIN dbo.OrganizationIndustryTypeLinks ON dbo.Organizations.cmoOrganizationID = dbo.OrganizationIndustryTypeLinks.cmdOrganizationID
) LEFT OUTER JOIN dbo.Calls ON dbo.Organizations.cmoOrganizationID = dbo.Calls.kbpOrganizationID
) LEFT OUTER JOIN dbo.CallLines ON dbo.Calls.kbpCallID = dbo.CallLines.kblCallID
)
WHERE dbo.Quotes.qmpQuoteID IS NULL
AND dbo.Organizations.cmoCustomerStatus = 1
AND dbo.OrganizationIndustryTypeLinks.cmdIndustryTypeID = 'DRAFTING '
AND (
dbo.CallLines.kblAddedDate < DATEADD(dd, - 60, GETDATE())
OR dbo.CallLines.kblCallID IS NULL
)
GROUP BY dbo.Organizations.cmoName
,dbo.Calls.kbpCallID
ORDER BY dbo.Organizations.cmoName ASC OFFSET 0 ROWS
FETCH 10 ROWS ONLY;
如果您使用 SQL 服务器,您将使用 top(10)
而不是 Limit 10
。