gpt4 book ai didi

php - 带条件的 GROUP BY/COUNT 简短查询

转载 作者:行者123 更新时间:2023-11-29 19:42:59 26 4
gpt4 key购买 nike

我在 php 中使用 for cicle 生成了此查询(替换列):

select count(`a`) as `countAdd`, `a` as `asd` from `question` where ( (`a` = 1 and ((`v` is not null and `b` not in (1,2)) or (`v` is null and `b` not in (6,7))) or (`a` = 2 and ((`v` is not null and `b` not in (2,3)) or (`v` is null and `b` not in (8,9))) or (`a` = 3 and ((`v` is not null and `b` not in (4,5)) or (`v` is null and `b` not in (8,4))) or (`a` = 4 and ((`v` is not null and `b` not in (4,9)) or (`v` is null and `b` not in (6,2))) or (`a` = 5 and ((`v` is not null and `b` not in (2,6)) or (`v` is null and `b` not in (3,4))) ) and `deleted_at` is null group by `addresseeId`

在 PHP 中

foreach ($a as $v)    $query .= '(`a` = ? and ((`b` is not null and `c` not in (?)) or (`a` is null `b` not in (?)))';

当我处理更大的数据时,如何避免查询这么长时间?

最佳答案

由于这个问题没有太多非技术背景,我会给你一般性建议。

有时,获取您需要的所有数据,然后在 php 本身中对其进行排序是有意义的。随着数据的增加,您正在构建的查询可能会变得非常大,因此我将避免在循环中构建查询。

解决您的问题的另一个可能的解决方案是重新设计您的数据库。如果您在某个时候注意到您的查询变得不适当的大,那么您的数据库设计可能存在一些问题。检查this link用于数据库设计(感谢@castis)

关于php - 带条件的 GROUP BY/COUNT 简短查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41226326/

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