Reputation: 41
Below is the SQL query of which Laravel query is required.
INSERT INTO 'details' (id, contact)
SELECT DISTINCT tmp_details.id, students.contact
FROM 'tmp_details'
INNER JOIN 'students' USING(name)
ON CONFLICT DO NOTHING;
I am not able to apply ON CONFLICT
condition.
I have partially converted it as below.
$strSQL = DB::table('tmp_details')
->join('students', 'tmp_details.name', '=', 'students.name')
->select('tmp_details.id','students.contact');
DB::table('details')->insertUsing(['id', 'contact'], $strSql);
Upvotes: 3
Views: 2287
Reputation: 220
Try use DB::select()
statement:
DB::select("
INSERT INTO 'details' (id, contact)
SELECT DISTINCT tmp_details.id, students.contact
FROM 'tmp_details'
INNER JOIN 'students' USING(name)
ON CONFLICT DO NOTHING;
");
Upvotes: 0
Reputation: 2292
Not sure about it but try this.
$strSQL = DB::table('tmp_details')
->join('students', 'tmp_details.name', '=', 'students.name')
->pluck('tmp_details.id','students.contact');
DB::table('details')->insertOrIgnore($strSql);
Upvotes: 1