【问题标题】:Laravel Raw SQL Queries with named bindings for multiple occurencesLaravel 原始 SQL 查询,具有多次出现的命名绑定
【发布时间】:2020-03-29 00:39:28
【问题描述】:

我正在使用 DB 外观和选择方法来运行带有绑定的原始 SQL 查询。只需要知道我们是否可以在查询中有多个同名的参数,并通过仅提供 1 个与参数名称的绑定来替换它。

例如

 $sql = "SELECT students.id, students.name FROM students 
                where students.student_id = :student_id 
                where added_on = ( SELECT MAX( added_on ) AS newdate
                FROM students WHERE student_id = :student_id)"

    return DB::select($sql, [
        'student_id' => 1
    ]);

注意:我可能不必在此查询中使用 student_id 两次。它只是一个例子。 目的是知道我们是否能做到这一点。

【问题讨论】:

标签: php database laravel laravel-5 laravel-query-builder


【解决方案1】:

我不相信它可能,这并不理想,但我通常会这样做:

$sql = "SELECT students.id, students.name FROM students 
                where students.student_id = :student_id_1
                where added_on = ( SELECT MAX( added_on ) AS newdate
                FROM students WHERE student_id = :student_id_2)"

    $student_id = 1;

    return DB::select($sql, [
        'student_id_1' => $student_id,
        'student_id_2' => $student_id
    ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-28
    • 2021-07-27
    • 2021-11-20
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    相关资源
    最近更新 更多