Laravel - 语法错误或访问冲突:1064 SQL语法错误

时间:2016-07-11 17:54:50

标签: 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代码在Error文本中就可以了。这是我将它粘贴到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
)

1 个答案:

答案 0 :(得分:1)

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

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

OR

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