我正在尝试将 Sql 查询转换为 Laravel 查询,但收到错误:?
SQLSTATE[HY000]: General error: 2809 General SQL Server error: Check messages from the SQL Server [2809] (severity 18) [(null)] (SQL: GetRegisteredUserData)
我的查询1:
SELECT * FROM RegisteredUser WHERE DateCreated between '$startDate' AND '$endDate' ORDER BY Yr DESC,MnthNo DESC
我的查询2:
SELECT * from UserData where((Yr * 10000 + Mnth * 100 + 1) BETWEEN $startDate AND $endDate) ORDER BY Yr Asc, Mnth Asc
最佳答案
如果 $startDate 和 $endDate 是日期或日期时间。 此查询将对您有所帮助。
SELECT * from UserData where
where cast(
left(cast(yr*10000+mnth*100+day as varchar),4)+'-'+substring(cast(yr*10000+mnth*100+day as varchar),5,2)+'-'+right(cast(yr*10000+mnth*100+day as varchar),2)
as datetime)
between $startDate AND $endDate
我的建议是创建一个函数。
create function fn_returndtae(@yr int,@mnth int,@day)
returns datetime
as
return
(
select
cast(
left(cast(@yr*10000+@mnth*100+@day as varchar),4)+'-'+substring(cast(@yr*10000+@mnth*100+@day as varchar),5,2)+'-'+right(cast(@yr*10000+@mnth*100+@day as varchar),2)
as datetime)
)
并在 WHERE 子句中使用。
SELECT * from UserData where
where db_name.dbo.fn_returndtae(yr,mnth,day)
between $startDate AND $endDate
谢谢
关于php - 我尝试将其转换为与 Laravel 的 DB 类一起使用,但它没有返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37880037/