Laravel 5.4 连接两个表
Laravel 5.4 joining two tables
我在这里很迷路。我正在尝试获取 Records table [= 中不存在的人员列表22=] 在 Profiles tables 上有特定的 course(例如心理学)和 year(例如 2011)。到目前为止,这就是我想出的,不幸的是它没有用。
$check = Record::join('Profiles', function ($join) {
$join->on('Records.firstname', '!=', 'Profiles.firstname');
$join->on('Records.lastname', '!=', 'Profiles.lastname');
$join->on('Records.middlename', '!=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->get();
dump($check);
有什么办法可以解决这个问题吗?我对此很陌生。提前致谢。关于加入 table 的建议和技巧将不胜感激。
我会考虑执行左外连接,然后选择配置文件名称为空的记录。
$check = Record::leftJoin('Profiles', function ($join) {
$join->on('Records.firstname', '=', 'Profiles.firstname');
$join->on('Records.lastname', '=', 'Profiles.lastname');
$join->on('Records.middlename', '=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->whereNull('Profiles.firstname')
->get();
请尝试 NOT EXISTS
子查询:
use Illuminate\Database\Query\Builder;
$check = \DB::table('Records')
->whereNotExists(function(Builder $query) {
$query->select(\DB::raw(1))
->from('Profiles')
->whereRaw('Records.firstname = Profiles.firstname')
->whereRaw('Records.middlename = Profiles.middlename')
->whereRaw('Records.lastname = Profiles.lastname');
})
->select('firstname', 'middlename', 'lastname')
->where('year', 2011)
->where('course', 'Psychology');
->get();
我在这里很迷路。我正在尝试获取 Records table [= 中不存在的人员列表22=] 在 Profiles tables 上有特定的 course(例如心理学)和 year(例如 2011)。到目前为止,这就是我想出的,不幸的是它没有用。
$check = Record::join('Profiles', function ($join) {
$join->on('Records.firstname', '!=', 'Profiles.firstname');
$join->on('Records.lastname', '!=', 'Profiles.lastname');
$join->on('Records.middlename', '!=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->get();
dump($check);
有什么办法可以解决这个问题吗?我对此很陌生。提前致谢。关于加入 table 的建议和技巧将不胜感激。
我会考虑执行左外连接,然后选择配置文件名称为空的记录。
$check = Record::leftJoin('Profiles', function ($join) {
$join->on('Records.firstname', '=', 'Profiles.firstname');
$join->on('Records.lastname', '=', 'Profiles.lastname');
$join->on('Records.middlename', '=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->whereNull('Profiles.firstname')
->get();
请尝试 NOT EXISTS
子查询:
use Illuminate\Database\Query\Builder;
$check = \DB::table('Records')
->whereNotExists(function(Builder $query) {
$query->select(\DB::raw(1))
->from('Profiles')
->whereRaw('Records.firstname = Profiles.firstname')
->whereRaw('Records.middlename = Profiles.middlename')
->whereRaw('Records.lastname = Profiles.lastname');
})
->select('firstname', 'middlename', 'lastname')
->where('year', 2011)
->where('course', 'Psychology');
->get();