laravel连接两张表查询如何只要某个字段和这个字段的统计集合

两张表只要name字段的值如“张三” 和叫“张三”的人的数量
2025-04-18 23:20:55
推荐回答(2个)
回答1:

--把tableA 中sign值为package 赋值给tableB update b set b.sign=a.sign from tableA a,tableB b where a.order_id=b.order_id and a.sign='package' --统计数量 select count(*) as 数量 from tableB where sign='package'

回答2:

你可以使用withCount()方法试一下

$posts = Post::withCount(['votes', 'comments' => function ($query) {
    $query->where('content', 'like', 'foo%');}])->get();
}])->get()