簡體   English   中英

計算 2 列的唯一條目

[英]counting of unique entries of 2 columns

我有下表:

Schema::create('tracks', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('user_id')->unsigned()->nullable()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('ip');
        $table->string('session');
        $table->string('roboname')->nullable();
        $table->string('language');
        $table->string('referer');
        $table->string('path');
        $table->string('device');
        $table->string('screensize');
        $table->string('browser');
        $table->string('browser_version');
        $table->string('os');
        $table->string('os_version');
        $table->string('url');
        $table->string('full_url');
        $table->boolean('is_mobile');
        $table->boolean('is_tablet');
        $table->timestamps();
    });

我想計算會話和路徑的所有唯一條目。 做這個的最好方式是什么? 例子:

session | path
123 /test
123 /test1
123 /test
321 /test
321 /test

這里的結果是 3。

假設您有 2 列session , path您可以將 count distinct 用於 concat 的session , path

 select count(distinct concat(session,path)) my_count 
 from  my_table 

您可以使用下一個解決方案:

SELECT COUNT(DISTINCT session, path) FROM tracks;

此查詢返回會話和路徑的不同組合的計數

你可以像這樣在count discount ifnull上使用count discount ifnull

Track::select(DB::raw("COUNT(DISTINCT IFNULL(session, ''), IFNULL(path, '')) AS session_path_count"))
      ->first()
      ->session_path_count;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM