【问题标题】:sql query for count the number which have same valuesql查询计算具有相同值的数字
【发布时间】:2017-04-06 20:46:17
【问题描述】:

我有患者就诊表,所以我必须知道特定患者访问医院的次数。我用过以下查询:

(select count(id) from visit where cid = cid) as vno.

所以,我想计算所有相同的 cid 值。并显示在“vno”列中。一名患者只有一个cid。因此,如果该特定患者一次又一次地访问。我需要知道该特定患者访问了多少次

下面我有访问页面的图片

【问题讨论】:

  • 能展示一下表格结构吗?

标签: laravel


【解决方案1】:

我认为它的工作

 $noOfVisit = DB::table('visit')->whereCid($cid)->count();
 dd($noOfVisit);

【讨论】:

    【解决方案2】:

    Patient 模型的hasMany 关系怎么样?

    public function visits()
    {
        return $this->hasMany(Visit::class, 'cid', 'cid');
    }
    

    那么在你看来

    {{ $patient->visits->count() }}
    

    【讨论】:

      【解决方案3】:
              $unique_visitors = Visit::pluck('cid')->unique()->toArray();
              $visitor_count = [];
              foreach($unique_visitors as $visitor_id)
              {
              $visitor_count[$visitor_id] = count(Visit::where('cid','=',$visitor_id)->get());
              }
      
      // output would be an array with key as cid and value as their visit counts
      // Something like this
          Array(
          [1] => 13;
          [2] => 6;
          )
      

      【讨论】:

        猜你喜欢
        • 2018-08-09
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        相关资源
        最近更新 更多