![](/img/trans.png)
[英]extracting and counting unique columns from an SQL query with multiple joins
[英]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.