混合OR&和mysql查询错误的结果(laravel)

时间:2015-10-21 21:24:07

标签: php mysql laravel eloquent

我正在使用eloquent查询构建器,这是特定查询产生的内容:

select * from `articles` where (select count(*) from `authors` where `authors`.`article_id` = `articles`.`id` and `author` like ?) >= 1 or `title` LIKE ? and `approved` = ? or (select count(*) from `tags` where `tags`.`article_id` = `articles`.`id` and `tag` like ?) >= 1 or `abstract` LIKE ? or `description` LIKE ? and `year` NOT LIKE ?

问题是这个查询仍然会产生像结果一样的年份结果,而不是像所述年份那样的结果。

这是我返回$query->toSql()时得到的结果。查询本身是使用Laravel Eloquent动态构建的,其中包含orWherewhereHasorWhereHas,但是如果我能理解错误的原因这个查询我应该能够更接近问题。

1 个答案:

答案 0 :(得分:0)

试试这个:

 SELECT *
FROM   `articles`
WHERE   (
        ((SELECT Count(*)
        FROM   `authors`
        WHERE  `authors`.`article_id` = `articles`.`id`
               AND `author` LIKE ?) >= 1)
        OR (`title` LIKE ?)

        OR ((SELECT Count(*)
            FROM   `tags`
            WHERE  `tags`.`article_id` = `articles`.`id`
                   AND `tag` LIKE ?) >= 1)
        OR (`abstract` LIKE ?)
        OR (`description` LIKE ?)
        )
        AND
        (`year` NOT LIKE ?
           AND `approved` = ?)