Hello_1234
Hello_1234

Reputation: 35

counting of unique entries of 2 columns

I have the following table:

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();
    });

i want to count all unique entries of session and path. What is the best way to do this? example:

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

The result here would be 3.

Upvotes: 0

Views: 39

Answers (3)

TsaiKoga
TsaiKoga

Reputation: 13394

You can use count discount ifnull on Eloquent like this:

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

Upvotes: 0

Slava Rozhnev
Slava Rozhnev

Reputation: 10163

You can use next solution:

SELECT COUNT(DISTINCT session, path) FROM tracks;

This query return count of distinct compositions of session and path

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133370

assuming you have 2 columns session , path you could use count distinct for a concat

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

Upvotes: 1

Related Questions