在 Laravel 查询生成器中检查表是否已经有 where 条件的实现
1、参考:在 MySQL 8.0 中,查询表记录总数时,超时的分析优化。当一条 count SQL 没有 where 条件存在时,就添加 WHERE id > 0。
2、参考:How to check if table is already has a where clause in Laravel Query Builder? 最终实现如下
1 2 3 | if ( empty ( $builder ->getQuery()->wheres)) { $builder ->where( 'id' , '>' , 0); } |
3、最终生成 SQL 如下,符合预期
1 | select count (*) as aggregate from `tables` where `id` > 0 |
近期评论