【问题标题】:How to get the columns with count of the same table in MySQL如何在MySQL中获取具有同一表计数的列
【发布时间】:2020-11-01 10:18:46
【问题描述】:

我在 laravel 上工作时遇到了麻烦,无法统计有多少用户赞助了这张名为 users 的表格,数据如下所示。

|--------------|----------------  |-------------|
|      id      |    first_name    | sponsor_id  |
|--------------|------------------|-------------|
|       59     |    kaqefizew     |   NULL      |
|--------------|------------------|-------------|
|       68     |    fucifaxe      |   59        |
|--------------|------------------|-------------|
|       81     |    fepetora      |   59        |
|--------------|------------------|-------------|
|       82     |    kixys         |   81        |
|--------------|------------------|-------------|

并且需要如下输出

|--------------|----------------  |------------------|
|      id      |    first_name    | sponsored_count  |
|--------------|------------------|------------------|
|       59     |    kaqefizew     |   2              |
|--------------|------------------|------------------|
|       68     |    fucifaxe      |   0              |
|--------------|------------------|------------------|
|       81     |    fepetora      |   1              |
|--------------|------------------|------------------|
|       82     |    kixys         |   0              |
|--------------|------------------|------------------|

我尝试使用计数子句

SELECT id, first_name, COUNT(sponsor_id) as sponsored_count FROM `users` GROUP BY id

但我得到如下错误输出

|--------------|----------------  |------------------|
|      id      |    first_name    | sponsored_count  |
|--------------|------------------|------------------|
|       59     |    kaqefizew     |   0              |
|--------------|------------------|------------------|
|       68     |    fucifaxe      |   1              |
|--------------|------------------|------------------|
|       81     |    fepetora      |   1              |
|--------------|------------------|------------------|
|       82     |    kixys         |   1              |
|--------------|------------------|------------------|

关系

class User extends Authenticatable implements JWTSubject
{

public function sponsor(  ){
        return $this->belongsTo( User::class, 'sponsor_id', 'id' );
    }
}

请帮我解决这个问题。

谢谢

【问题讨论】:

  • 你能分享更多关于你的模特和他们的关系的信息吗?

标签: mysql sql laravel eloquent count


【解决方案1】:

一个选项使用子查询:

select id, first_name,
    (select count(*) from mytable t1 where t1.sponsor_id = t.id) sponsored_count
from mytable t

您也可以left join 聚合查询:

select t.id, t.first_name, t1.sponsored_count
from mytable t
left join (
    select sponsor_id, count(*) sponsored_count
    from mytable
    group by sponsor_id
) t1 on t1.sponsor_id = t.id

【讨论】:

  • 非常感谢你能帮助我如何实现它 laravel 雄辩,因为我是新手。
【解决方案2】:

首先,编辑您的关系方法以引用赞助商模型,如下所示:

public function sponsor(  ){
        return $this->belongsTo( Sponsor::class, 'sponsor_id', 'id' );
    }

您可以使用 withCount 静态方法以 eloquent 计算关系。

$users=User::withCount('sponsor')->get();

然后你可以通过调用$users->sponsors_count来获取计数

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2021-08-05
    • 2019-01-11
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    相关资源
    最近更新 更多