OrderBy 子句不适用于 Microsoft SQL?
OrderBy clause not working on Microsoft SQL?
我最近试图在我的 Laravel 网络应用程序上将我的数据库从 MariaDB 更改为 SQL 服务器(不幸的是不是我的电话)。但是,当我 运行 并打开其中一个页面时,我收到了错误消息:
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
还有这个:
SELECT COUNT(*) AS aggregate
FROM [items]
INNER JOIN (SELECT [model_name],
MAX(version) AS max_version
FROM [items]
WHERE [items].[deleted_at] IS NULL
GROUP BY [model_name]
ORDER by [created_at] ASC) [max_version_table] ON [items].[model_name] = [max_version_table].[model_name]
AND [items].[version] = [max_version_table].[max_version]
WHERE [items].[deleted_at] IS NULL;
我是 SQL 的新手,这是我的 Eloquent ORM 代码:
public function AllModel(){
$max_version_table = Item::select('model_name', DB::raw('MAX(version) as max_version'))->orderBy('created_at')->groupBy('model_name');
$items = Item::select('*')->joinSub($max_version_table, 'max_version_table', function($join){
$join->on('items.model_name','=','max_version_table.model_name');
$join->on('items.version','=','max_version_table.max_version');
})->paginate(5); //Shows max version of each model name
return view('admin.item.index',compact('items'));
}
我的理解是 MSSql 不支持 OrderBy,因为它在 MariaDB 上运行得很好。如果可以,请问我可以用它换什么?感谢任何帮助
SQL 服务器绝对支持 ORDER BY,但您的查询应该类似于
select count(*) as aggregate
from [items]
inner join
(
select [model_name], MAX(version) as max_version from [items] where [items]. [deleted_at] is null group by [model_name]
) as [max_version_table]
on
[items].[model_name] = [max_version_table].[model_name]
and [items].[version] = [max_version_table].[max_version]
where [items].[deleted_at] is null
MSSQL 肯定支持 order by
但在你的情况下,错误已经提示
The ORDER BY
clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions
您不能像以前那样在 subqueries
中使用 order by
。
浏览 mssql order by documentation 以获得更多理解
通过查看您的查询,我认为没有必要添加任何 order by
,因为它什么都不做。您可以删除该子句。
SELECT count(*) AS AGGREGATE
FROM [items]
INNER JOIN
(SELECT [model_name],
MAX(VERSION) AS max_version
FROM [items]
WHERE [items].[deleted_at] IS NULL
GROUP BY [model_name]
) AS [max_version_table] ON [items].[model_name] = [max_version_table].[model_name]
AND [items].[version] = [max_version_table].[max_version]
WHERE [items].[deleted_at] IS NULL
我最近试图在我的 Laravel 网络应用程序上将我的数据库从 MariaDB 更改为 SQL 服务器(不幸的是不是我的电话)。但是,当我 运行 并打开其中一个页面时,我收到了错误消息:
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
还有这个:
SELECT COUNT(*) AS aggregate
FROM [items]
INNER JOIN (SELECT [model_name],
MAX(version) AS max_version
FROM [items]
WHERE [items].[deleted_at] IS NULL
GROUP BY [model_name]
ORDER by [created_at] ASC) [max_version_table] ON [items].[model_name] = [max_version_table].[model_name]
AND [items].[version] = [max_version_table].[max_version]
WHERE [items].[deleted_at] IS NULL;
我是 SQL 的新手,这是我的 Eloquent ORM 代码:
public function AllModel(){
$max_version_table = Item::select('model_name', DB::raw('MAX(version) as max_version'))->orderBy('created_at')->groupBy('model_name');
$items = Item::select('*')->joinSub($max_version_table, 'max_version_table', function($join){
$join->on('items.model_name','=','max_version_table.model_name');
$join->on('items.version','=','max_version_table.max_version');
})->paginate(5); //Shows max version of each model name
return view('admin.item.index',compact('items'));
}
我的理解是 MSSql 不支持 OrderBy,因为它在 MariaDB 上运行得很好。如果可以,请问我可以用它换什么?感谢任何帮助
SQL 服务器绝对支持 ORDER BY,但您的查询应该类似于
select count(*) as aggregate
from [items]
inner join
(
select [model_name], MAX(version) as max_version from [items] where [items]. [deleted_at] is null group by [model_name]
) as [max_version_table]
on
[items].[model_name] = [max_version_table].[model_name]
and [items].[version] = [max_version_table].[max_version]
where [items].[deleted_at] is null
MSSQL 肯定支持 order by
但在你的情况下,错误已经提示
The
ORDER BY
clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions
您不能像以前那样在 subqueries
中使用 order by
。
浏览 mssql order by documentation 以获得更多理解
通过查看您的查询,我认为没有必要添加任何 order by
,因为它什么都不做。您可以删除该子句。
SELECT count(*) AS AGGREGATE
FROM [items]
INNER JOIN
(SELECT [model_name],
MAX(VERSION) AS max_version
FROM [items]
WHERE [items].[deleted_at] IS NULL
GROUP BY [model_name]
) AS [max_version_table] ON [items].[model_name] = [max_version_table].[model_name]
AND [items].[version] = [max_version_table].[max_version]
WHERE [items].[deleted_at] IS NULL