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

标签 php mysql sql-server laravel-5

我正在尝试将 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/

相关文章:

sql-server - SQL : left join and sum: Arithmetic overflow error converting expression to data type int

php - 显示选择查询数据,但我想用子查询创建新表选择它很好,但问题是查询运行需要 30 分钟

php - 从另一个 php 执行 php 文件

php - 将 NULL 显示为无文本

mysql - WordPress mysql 密码哈希

Sql查询,其中x是两列之间的值

sql-server - 非聚集索引上的Where子句与聚集索引上的额外连接和where子句

php - mysql_fetch_array() 带有奇怪的输出

MySql 插入两个用外键连接的表

php - 从mysql数据库获取一个 "cell"