作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决一个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/
我是一名优秀的程序员,十分优秀!