如何使用 eloquent 或原始查询使用来自 table 的数据查询不同的月份
How to query distinct months with it's data from the table using eloquent or raw query
我有我的 table 叫出勤,其中有列
Schema::create('attendances', function (Blueprint $table) {
$table->increments('id');
$table->string('school_registration_number');
$table->integer('students')->default(0);
$table->integer('books_borrowed')->default(0);
$table->integer('fiction_english')->default(0);
$table->integer('fiction_swahili')->default(0);
$table->integer('chem')->default(0);
$table->integer('bio')->default(0);
$table->integer('geo')->default(0);
$table->integer('math')->default(0);
$table->integer('civ')->default(0);
$table->integer('kis')->default(0);
$table->integer('his')->default(0);
$table->integer('eng')->default(0);
$table->string('date');
$table->string('month');
$table->string('year');
});
现在我想查询 2017 年年度报告示例,我可以在其中获取一年中不同的月份,并在每个月的行中显示学生总和,2017 年那个月借的每本书的总和
这应该有效:
<?php
$sql = <<<SQL
SELECT DISTINCT COUNT(students), COUNT(books_borrowed), month, year
FROM attendances
WHERE year = '2017'
SQL;
$result = DB::select(DB::raw($sql));
我能够针对我的问题编写查询
$sql = "SELECT DISTINCT month, sum(students) as students, sum(books_borrowed) as books_borrowed, sum(fiction_english) as feng,sum(fiction_swahili) as fkis,sum(chem) as chem,sum(bio) as bio, sum(geo) as geo,sum(math) as math, sum(civ) as civ,sum(kis) as kis,sum(his) as his,sum(eng) as eng
FROM attendances
WHERE year = $year AND school_registration_number = '$regno' GROUP BY month";
$result = DB::select(DB::raw($sql));
我有我的 table 叫出勤,其中有列
Schema::create('attendances', function (Blueprint $table) {
$table->increments('id');
$table->string('school_registration_number');
$table->integer('students')->default(0);
$table->integer('books_borrowed')->default(0);
$table->integer('fiction_english')->default(0);
$table->integer('fiction_swahili')->default(0);
$table->integer('chem')->default(0);
$table->integer('bio')->default(0);
$table->integer('geo')->default(0);
$table->integer('math')->default(0);
$table->integer('civ')->default(0);
$table->integer('kis')->default(0);
$table->integer('his')->default(0);
$table->integer('eng')->default(0);
$table->string('date');
$table->string('month');
$table->string('year');
});
现在我想查询 2017 年年度报告示例,我可以在其中获取一年中不同的月份,并在每个月的行中显示学生总和,2017 年那个月借的每本书的总和
这应该有效:
<?php
$sql = <<<SQL
SELECT DISTINCT COUNT(students), COUNT(books_borrowed), month, year
FROM attendances
WHERE year = '2017'
SQL;
$result = DB::select(DB::raw($sql));
我能够针对我的问题编写查询
$sql = "SELECT DISTINCT month, sum(students) as students, sum(books_borrowed) as books_borrowed, sum(fiction_english) as feng,sum(fiction_swahili) as fkis,sum(chem) as chem,sum(bio) as bio, sum(geo) as geo,sum(math) as math, sum(civ) as civ,sum(kis) as kis,sum(his) as his,sum(eng) as eng
FROM attendances
WHERE year = $year AND school_registration_number = '$regno' GROUP BY month";
$result = DB::select(DB::raw($sql));