在SQL Server中,我会用这个查询来定义、设置和使用一个变量:
declare @teacherId bigint -- declaring
select @teacherId = Id from Teachers where [Name] = 'John' -- filling/setting
select * from Students where TeacherId = @teacherId -- using
我如何在MariaDB中写?我陷入了困境。我不断地收到错误,文档也非常没有帮助,而且缺乏示例。我试过了:
declare @teacherId
select Id into @teacherId from Teachers where `Name` = 'John'
但它也有错误。
更新:
错误为:
查询中的错误(1064(:"declare@teacherId select Id into@teacher Id from Teachers where
Name
…"附近的语法错误在第1行
您不需要变量就可以做到这一点。加入表格
select *
from Students
join Teachers
on Students.TeacherID = Students.TeacherID
where Teachers.Name= 'John'
MariaDB确实有存储过程,您可以在其中使用变量。请参阅他们的文档。https://mariadb.com/kb/en/stored-routines/