gpt4 book ai didi

SQL错误 "The ORDER BY clause is invalid in views..."

转载 作者:行者123 更新时间:2023-12-01 01:29:04 26 4
gpt4 key购买 nike

我正在尝试解决一个SQL Server错误。有人可以帮我吗?
查询是:

SELECT TOP 10 * 
FROM (
SELECT c.id, c.name, c.inserteddate, c.cityname, ftblstates.name AS statename, clc.name AS catname, '' AS listingimagelogo, '' AS orgname, relocateyn, '' AS employerclassified
FROM ((tblclassifieds c
LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id)
LEFT JOIN ftblstates ON c.stateid = ftblstates.id)
WHERE (c.expirydate != '') AND NOT c.id IN (
SELECT TOP 10 tblclassifieds.id
FROM tblclassifieds
WHERE (c.expirydate != '')
ORDER BY inserteddate desc)
UNION ALL
SELECT ce.id, ce.name, ce.inserteddate, suburb AS cityname, ftblstates.name AS statename, ce.jobtype AS catname, ce.listingimagelogo, ce.orgname, '' AS relocateyn, '1' AS employerclassified
FROM tblclassifiedemployers ce
LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
WHERE (ce.expirydate != '') AND NOT ce.id IN (
SELECT TOP 10 tblclassifiedemployers.id
FROM tblclassifiedemployers
WHERE (ce.expirydate != '')
ORDER BY inserteddate desc)
ORDER BY inserteddate desc;
错误:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

最佳答案

如前所述,除非使用TOP或FOR XML,否则ORDER BY不得出现在子查询中。

    SELECT TOP 10 * FROM ( 
SELECT
c.id,
c.name,
c.inserteddate,
c.cityname,
ftblstates.name AS statename,
clc.name AS catname,
'' AS listingimagelogo,
'' AS orgname, relocateyn,
'' AS employerclassified
FROM tblclassifieds c
LEFT JOIN tblclassifiedscategories clc ON c.categoryid = clc.id
LEFT JOIN ftblstates ON c.stateid = ftblstates.id
WHERE c.expirydate != ''
AND NOT c.id IN (
SELECT TOP 10
tblclassifieds.id
FROM tblclassifieds
WHERE c.expirydate != ''
ORDER BY inserteddate desc
)
UNION ALL
SELECT
ce.id,
ce.name,
ce.inserteddate,
suburb AS cityname,
ftblstates.name AS statename,
ce.jobtype AS catname,
ce.listingimagelogo,
ce.orgname, '' AS relocateyn,
'1' AS employerclassified
FROM tblclassifiedemployers ce
LEFT JOIN ftblstates ON ce.stateid = ftblstates.id
WHERE ce.expirydate != ''
AND NOT ce.id IN (
SELECT TOP 10
tblclassifiedemployers.id
FROM tblclassifiedemployers
WHERE ce.expirydate != ''
ORDER BY inserteddate desc
)
) a ORDER BY inserteddate desc;

关于SQL错误 "The ORDER BY clause is invalid in views...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5945851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com