php - Laravel - 语法错误或访问冲突 : 1064 You have an error in your SQL syntax

标签 php mysql laravel

我的问题有点神秘。如果我使用 PHPmyadmin 尝试,sql 代码的输出可以工作,但它不适用于 laravel。

这是查询生成器代码

$product= Product::leftJoin('ProductTransaction as PT',function($query){
                $query->on('PT.ProductId','=','Product.id');
                $todaysDate=Carbon::today()->toDateString();
                $query->Where(DB::raw("date(`PT`.`CreatedAt`)=$todaysDate"));

            })->Where(function($query) use($RetailerIds){
                if (count($RetailerIds)) {
                    $query->WhereIn('RetailerId',$RetailerIds);
                }
            })->Where(function($query)use($Genders){
                if (count($Genders)) {
                    $query->WhereIn('Gender',$Genders);
                }
            })->WhereHas("Stocks",function($query) use($Color){

                if ($Color!='') {
                    $query->WhereHas('Color',function($query) use($Color){
                        $query->Where('Color','like',"%$Color%");
                    });
                }

            })->Where(function($query) use ($q){

                if ($q!='') {
                    $query->Where('ProductTitle','like','%'.$q.'%');
                    $query->orWhere('Description','like','%'.$q.'%');
                }

            })->WhereHas('Stocks',function($query) {
                $query->Where('Stock','!=',-1);
                $query->WhereDate('CreatedAt','=',Carbon::today()->toDateString());
            })->WhereHas('ProductTransactions',function($query) use($MinDiscountPrice,$MaxDiscountPrice,$todaysMaxDiscountPrice,$todaysMinDiscountPrice){

                $query->WhereDate('Product.CreatedAt','=',Carbon::today()->toDateString());

                if ($todaysMinDiscountPrice!=$MinDiscountPrice) {
                    $query->Where('DiscountPrice','>=',$MinDiscountPrice);
                }

                if ($todaysMaxDiscountPrice!=$MaxDiscountPrice) {
                    $query->Where('DiscountPrice','<=',$MaxDiscountPrice);
                }
            })->With(['ProductTransactions','ProductImages','ProductCategory','Retailer','Stocks.Size'])->OrderBy($orderBy)->paginate(15);

这是此查询填充的错误代码:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? where (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id`' at line 1 (SQL: select count(*) as aggregate from `Product` left join `ProductTransaction` as `PT` on `PT`.`ProductId` = `Product`.`id` and date(`PT`.`CreatedAt`)=2016-07-11 where (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id`) >= 1 and (`ProductTitle` like %test% or `Description` like %test%) and (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id` and `Stock` != -1 and date(`CreatedAt`) = 2016-07-11) >= 1 and (select count(*) from `ProductTransaction` where `ProductTransaction`.`ProductId` = `Product`.`id` and date(`Product`.`CreatedAt`) = 2016-07-11) >= 1)

但最有趣的一点是,如果我将 SQL 代码复制并粘贴到错误文本中,它就会起作用。这是我将其粘贴到 PHPMyadmin 的 SQL 代码

SELECT
    count(*) AS AGGREGATE
FROM
    `Product`
LEFT JOIN `ProductTransaction` AS `PT` ON `PT`.`ProductId` = `Product`.`id`
AND date(`PT`.`CreatedAt`) = 2016 - 07 - 11
WHERE
    (
        SELECT
            count(*)
        FROM
            `Stock`
        WHERE
            `Stock`.`ProductId` = `Product`.`id`
    ) >= 1
AND (
    `ProductTitle` LIKE % test %
    OR `Description` LIKE % test %
)
AND (
    SELECT
        count(*)
    FROM
        `Stock`
    WHERE
        `Stock`.`ProductId` = `Product`.`id`
    AND `Stock` != - 1
    AND date(`CreatedAt`) = 2016 - 07 - 11
) >= 1
AND (
    SELECT
        count(*)
    FROM
        `ProductTransaction`
    WHERE
        `ProductTransaction`.`ProductId` = `Product`.`id`
    AND date(`Product`.`CreatedAt`) = 2016 - 07 - 11
) >= 1
)

最佳答案

需要引用您的日期。将其作为变量传递,或引用它:

$query->Where(DB::raw("date(`PT`.`CreatedAt`)='$todaysDate'"));

或者

$query->Where(DB::raw("date(`PT`.`CreatedAt`)=?",[$todaysDate])); // Preferable, so that the database can handle it

关于php - Laravel - 语法错误或访问冲突 : 1064 You have an error in your SQL syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313231/

相关文章:

php - 如何使用where条件获取yii2中表格的第一行?

c# - Serenity-将表单中的值添加到 2 个表中

mysql - 在没有临时表的情况下删除mysql中的重复条目

php - Laravel Eloquent 地获取所有记录,其中包含多对多关系中的所有 ID

php - Apache JMeter 不执行 HTML 页面中的 Javascript

php - 删除时触发 MySQL 状态更新

mysql - Docker设置mysql db

php - 调用未定义的函数 Project\Http\Controllers\array_sort()

laravel - env laravel 中的 SESSION_DRIVER=file 和 SESSION_DRIVER=redis 有什么区别?

php - Postgresql INSERT INTO 使用 PHP 不工作