将原始 SQL 转换为 Laravel 查询生成器
Convert raw SQL to Laravel Query Builder
我在将此 SQL 查询转换为 Laravel 查询时遇到困难 Builder.I 尝试了几个小时但无法理解 this.I 尝试了在线工具将 SQL 转换为查询生成器但没有成功。
这是我的代码:
SELECT
technologies.name_en,
Count(cig_members.id) AS CigTotal,
Count(CASE
WHEN cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnic,
Count(CASE
WHEN cig_members.gender = 'female' THEN 1
ELSE NULL
END) AS CigTotalFemale,
Count(CASE
WHEN cig_members.gender = 'female'
AND cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnicFemale,
Count(farmers.id) AS NonCigTotal,
Count(CASE
WHEN farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtoTalEthnic,
Count(CASE
WHEN farmers.gender = 'female' THEN 1
ELSE NULL
END) AS NonCigTotalFemale,
Count(CASE
WHEN farmers.gender = 'female'
AND farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtTotalEthnicFemale
FROM adopting_technologies
JOIN adopting_farmers
ON adopting_farmers.id = adopting_technologies.adopting_farmer_id
LEFT JOIN cig_members
ON cig_members.id = adopting_farmers.cig_member_id
LEFT JOIN farmers
ON farmers.id = adopting_farmers.farmer_id
LEFT JOIN financial_years
ON financial_years.id = adopting_farmers.financial_year_id
LEFT JOIN technologies
ON technologies.id = adopting_technologies.technology_id
GROUP BY adopting_technologies.technology_id
如何转换?
DB::table('adopting_technologies')
->select('adopting_technologies.technology_id', 'technologies.name_en')
->selectRaw("
COUNT(cig_members.id) AS CigTotal,
COUNT(CASE WHEN cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnic,
COUNT(CASE WHEN cig_members.gender = 'female' THEN 1 ELSE NULL END) AS CigTotalFemale,
COUNT(CASE WHEN cig_members.gender = 'female' AND cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnicFemale,
COUNT(farmers.id) AS NonCigTotal,
COUNT(CASE WHEN farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtoTalEthnic,
COUNT(CASE WHEN farmers.gender = 'female' THEN 1 ELSE NULL END) AS NonCigTotalFemale,
COUNT(CASE WHEN farmers.gender = 'female' AND farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtTotalEthnicFemale
")
->join('adopting_farmers', 'adopting_farmers.id', '=', 'adopting_technologies.adopting_farmer_id')
->leftJoin('cig_members', 'cig_members.id', '=', 'adopting_farmers.cig_member_id')
->leftJoin('farmers', 'farmers.id', '=', 'adopting_farmers.farmer_id')
->leftJoin('financial_years', 'financial_years.id', '=', 'adopting_farmers.financial_year_id')
->leftJoin('technologies', 'technologies.id', '=', 'adopting_technologies.technology_id')
->groupBy('adopting_technologies.technology_id')
->get();
大致应该是这样的:
$result = AdoptingTechnology::selectRaw("
technologies.name_en,
Count(cig_members.id) AS CigTotal,
Count(CASE
WHEN cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnic,
Count(CASE
WHEN cig_members.gender = 'female' THEN 1
ELSE NULL
END) AS CigTotalFemale,
Count(CASE
WHEN cig_members.gender = 'female'
AND cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnicFemale,
Count(farmers.id) AS NonCigTotal,
Count(CASE
WHEN farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtoTalEthnic,
Count(CASE
WHEN farmers.gender = 'female' THEN 1
ELSE NULL
END) AS NonCigTotalFemale,
Count(CASE
WHEN farmers.gender = 'female'
AND farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtTotalEthnicFemale
")
->join('adopting_farmers', 'adopting_farmers.id','adopting_technologies.adopting_farmer_id')
->leftJoin('cig_members','cig_members.id','adopting_farmers.cig_member_id')
->leftJoin('farmers','farmers.id','adopting_farmers.farmer_id')
->leftJoin('financial_years','financial_years.id','adopting_farmers.financial_year_id')
->leftJoin('technologies','technologies.id','adopting_technologies.technology_id')
->groupBy('adopting_technologies.technology_id')
->get();
如果您有任何问题请告诉我:) ofc 无法在本地进行测试
class adopting_technologies extends Model
{
public function newCollection(array $models = [])
{
return new FarmerCollection($models);
}
public function adopting_farmers()
{
return $this->belongsTo(adopting_farmers::class);
}
// other relations for 'cig_members', 'farmers', 'financial_years' and 'technologies'
}
namespace App\Collections;
use Illuminate\Database\Eloquent\Collection;
class FarmerCollection extends Collection
{
public function cigTotal()
{
return $this->pluck('cig_members')->map(function($items) { return $items->count(); })->sum();
}
// other totals for cigTotalEthnic, CigTotalFemale, CigTotalEthnicFemale, NonCigTotal, NonCigtoTalEthnic, NonCigTotalFemale, NonCigtTotalEthnicFemale
}
我在将此 SQL 查询转换为 Laravel 查询时遇到困难 Builder.I 尝试了几个小时但无法理解 this.I 尝试了在线工具将 SQL 转换为查询生成器但没有成功。
这是我的代码:
SELECT
technologies.name_en,
Count(cig_members.id) AS CigTotal,
Count(CASE
WHEN cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnic,
Count(CASE
WHEN cig_members.gender = 'female' THEN 1
ELSE NULL
END) AS CigTotalFemale,
Count(CASE
WHEN cig_members.gender = 'female'
AND cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnicFemale,
Count(farmers.id) AS NonCigTotal,
Count(CASE
WHEN farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtoTalEthnic,
Count(CASE
WHEN farmers.gender = 'female' THEN 1
ELSE NULL
END) AS NonCigTotalFemale,
Count(CASE
WHEN farmers.gender = 'female'
AND farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtTotalEthnicFemale
FROM adopting_technologies
JOIN adopting_farmers
ON adopting_farmers.id = adopting_technologies.adopting_farmer_id
LEFT JOIN cig_members
ON cig_members.id = adopting_farmers.cig_member_id
LEFT JOIN farmers
ON farmers.id = adopting_farmers.farmer_id
LEFT JOIN financial_years
ON financial_years.id = adopting_farmers.financial_year_id
LEFT JOIN technologies
ON technologies.id = adopting_technologies.technology_id
GROUP BY adopting_technologies.technology_id
如何转换?
DB::table('adopting_technologies')
->select('adopting_technologies.technology_id', 'technologies.name_en')
->selectRaw("
COUNT(cig_members.id) AS CigTotal,
COUNT(CASE WHEN cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnic,
COUNT(CASE WHEN cig_members.gender = 'female' THEN 1 ELSE NULL END) AS CigTotalFemale,
COUNT(CASE WHEN cig_members.gender = 'female' AND cig_members.is_ethnic = 1 THEN 1 ELSE NULL END) AS CigTotalEthnicFemale,
COUNT(farmers.id) AS NonCigTotal,
COUNT(CASE WHEN farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtoTalEthnic,
COUNT(CASE WHEN farmers.gender = 'female' THEN 1 ELSE NULL END) AS NonCigTotalFemale,
COUNT(CASE WHEN farmers.gender = 'female' AND farmers.is_ethnic = 1 THEN 1 ELSE NULL END) AS NonCigtTotalEthnicFemale
")
->join('adopting_farmers', 'adopting_farmers.id', '=', 'adopting_technologies.adopting_farmer_id')
->leftJoin('cig_members', 'cig_members.id', '=', 'adopting_farmers.cig_member_id')
->leftJoin('farmers', 'farmers.id', '=', 'adopting_farmers.farmer_id')
->leftJoin('financial_years', 'financial_years.id', '=', 'adopting_farmers.financial_year_id')
->leftJoin('technologies', 'technologies.id', '=', 'adopting_technologies.technology_id')
->groupBy('adopting_technologies.technology_id')
->get();
大致应该是这样的:
$result = AdoptingTechnology::selectRaw("
technologies.name_en,
Count(cig_members.id) AS CigTotal,
Count(CASE
WHEN cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnic,
Count(CASE
WHEN cig_members.gender = 'female' THEN 1
ELSE NULL
END) AS CigTotalFemale,
Count(CASE
WHEN cig_members.gender = 'female'
AND cig_members.is_ethnic = 1 THEN 1
ELSE NULL
END) AS CigTotalEthnicFemale,
Count(farmers.id) AS NonCigTotal,
Count(CASE
WHEN farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtoTalEthnic,
Count(CASE
WHEN farmers.gender = 'female' THEN 1
ELSE NULL
END) AS NonCigTotalFemale,
Count(CASE
WHEN farmers.gender = 'female'
AND farmers.is_ethnic = 1 THEN 1
ELSE NULL
END) AS NonCigtTotalEthnicFemale
")
->join('adopting_farmers', 'adopting_farmers.id','adopting_technologies.adopting_farmer_id')
->leftJoin('cig_members','cig_members.id','adopting_farmers.cig_member_id')
->leftJoin('farmers','farmers.id','adopting_farmers.farmer_id')
->leftJoin('financial_years','financial_years.id','adopting_farmers.financial_year_id')
->leftJoin('technologies','technologies.id','adopting_technologies.technology_id')
->groupBy('adopting_technologies.technology_id')
->get();
如果您有任何问题请告诉我:) ofc 无法在本地进行测试
class adopting_technologies extends Model
{
public function newCollection(array $models = [])
{
return new FarmerCollection($models);
}
public function adopting_farmers()
{
return $this->belongsTo(adopting_farmers::class);
}
// other relations for 'cig_members', 'farmers', 'financial_years' and 'technologies'
}
namespace App\Collections;
use Illuminate\Database\Eloquent\Collection;
class FarmerCollection extends Collection
{
public function cigTotal()
{
return $this->pluck('cig_members')->map(function($items) { return $items->count(); })->sum();
}
// other totals for cigTotalEthnic, CigTotalFemale, CigTotalEthnicFemale, NonCigTotal, NonCigtoTalEthnic, NonCigTotalFemale, NonCigtTotalEthnicFemale
}