gpt4 book ai didi

php - 混合 OR & AND mysql 查询错误结果 (laravel)

转载 作者:行者123 更新时间:2023-11-29 21:54:51 24 4
gpt4 key购买 nike

我正在使用 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 动态构建的,混合了 where、orWherewhereHas orWhereHas,但是如果我能理解这个查询出了什么问题,我应该能够更接近问题。

最佳答案

试试这个:

 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` = ?)

关于php - 混合 OR & AND mysql 查询错误结果 (laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com