【问题标题】:How to select count with laravel如何使用 laravel 选择计数
【发布时间】:2015-01-24 05:52:06
【问题描述】:

我想显示存储在数据库表中的数据量 Laravel v 4.2.0 但我在使用函数 count() 时遇到问题。这是我的代码:

$total = DB::select("select count(*) from rents left join operators on operators.code = rents.code where (operators.code is null) and rents.booking = 'N'");    
echo $total;

消息错误说:

Syntax error or access violation

谁能帮帮我?

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    试试这个:

    $total = DB::select(DB::raw("select count(*) as cnt from rents left join operators on operators.code = rents.code where (operators.code is null) and rents.booking = 'N'"));    
    echo $total[0]->cnt;
    

    尝试调试:

    echo "<pre>";
    print_r($total);
    echo "</pre>";
    exit;
    

    看看,如果这有帮助。

    【讨论】:

    • 您的 SQL 查询是否为您提取结果?
    • 我已经编辑了我的答案,尝试调试并查看您得到的输出,并将该输出附加到问题中。
    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 2012-10-24
    • 2016-04-13
    • 2022-08-10
    • 2020-03-15
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多