问题
Hi I want to get data from database, I am using join query but I have got this error:
QueryException in Connection.php line 673: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'surat_masuk' (SQL: select
jenis_surat
.jenis_surat
,surat_masuk
.nomor_surat
,surat_masuk
.perihal
,surat_masuk
.tanggal_publish
fromsurat_masuk
inner joinjenis_surat
onid_jenis_surat
=jenis_surat
.id_jenis_surat
inner joinsurat_masuk
onid_jenis_surat
=surat_masuk
.id_jenis_surat
)
I have 2 tables, surat_masuk and jenis_surat, I want to get jenis_surat field from jenis_surat table and nomor_surat, perihal, tanggal_publish from surat_masuk table. and there is column id_jenis_surat in both table. This is my query:
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->join('surat_masuk', 'id_jenis_surat', '=', 'surat_masuk.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
Do you know how to fix it?
回答1:
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')->get();
回答2:
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
OR
$surats = \DB::table('surat_masuk')
->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=','jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
来源:https://stackoverflow.com/questions/36759692/how-to-make-sql-join-query-in-laravel-5