我尝试将其转换为与Laravel的DB类一起使用,除非它没有返回任何内容

时间:2016-06-17 11:07:10

标签: php mysql sql-server laravel-5

我正在尝试将我的Sql查询转换为Laravel查询,我收到错误:?

  

SQLSTATE [HY000]:常规错误:2809常规SQL Server错误:检查来自SQL Server的消息[2809](严重级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

2 个答案:

答案 0 :(得分:0)

如果$ 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

由于

答案 1 :(得分:0)

在laravel 5中,我们必须执行以下操作才能从控制器执行查询 $ query = DB :: connection(' sqlsrv') - >选择(' SELECT * FROM RegisteredUser WHERE((Yr * 10000 + Mnth * 100 + 1)BETWEEN'。$ startDate。' AND'。$ endDate。')');

$ startDate = 20160101 $ endDate = 20160601