我有以下复杂查询,并希望在Laravel中使用它。但直到现在我还没有成功。也许你们中的一个人可以展示如何翻译"它是Laravel的查询构建器。
此处Workbench中的原始查询运行良好:
select * from lemma l
inner join etymology e on l.id=e.lemma_id_fk
inner join gloss g on e.id = g.etymology_id
inner join wold_meanings w on g.gloss=w.meaning
where g.gloss like '%flower%' #paper, ocean, etc.
limit 100;
我尝试了以下内容:
$results = DB::table('lemma')
->join('etymology', 'lemma.id', '=', 'etymology.lemma_id_fk')
->join('gloss', 'etymology.id', '=', 'gloss.etymology_id')
->join('wold_meanings', 'gloss.gloss', '=', 'wold_meanings.meaning')
->select(DB::raw('lemma.*'))
->where('gloss.gloss', 'like', '%flower%')
->get();
还有:
$results = DB::table('lemma')
->select(DB::raw("select * from lemma l
inner join etymology e on l.id=e.lemma_id_fk
inner join gloss g on e.id = g.etymology_id
inner join wold_meanings w on g.gloss=w.meaning
where g.gloss like '%flower%'
limit 1"));
这也是:
$results = DB::raw("select * from lemma l
inner join etymology e on l.id=e.lemma_id_fk
inner join gloss g on e.id = g.etymology_id
inner join wold_meanings w on g.gloss=w.meaning
where g.gloss like '%flower%'
limit 100");
但我绝对没有成功......: - (
有人向我展示正确的方法吗?我使用的是Laravel 5.4。
==编辑#1 ==
此处应显示结果的视图:
<table class="table table-bordered table-condensed table-hover table-responsive table-striped" id="table">
<thead>
<tr>
<th>id</th>
<th>lemma_id</th>
<th>headword</th>
<th>lemma</th>
<th>pos</th>
<th>gender</th>
<th>language</th>
<th>origin_family</th>
<th>origin</th>
<th>short_path</th>
<th>origin_path</th>
<th>etymology_text</th>
<th>first_use</th>
<th>lang</th>
<th>pageid</th>
<th>term</th>
<th>non_latin_script</th>
<th>lang_2</th>
<th>gloss</th>
<th>sequence</th>
<th>lemma_id_fk</th>
<th>derivatives_id</th>
<th>etymology_id</th>
<th>meaning</th>
<th>semantic_category</th>
<th>semantic_field</th>
<th>simplicity_score</th>
<th>age_score</th>
<th>borrowed_score</th>
<th>description</th>
<th>typical_context</th>
<th>representation</th>
<th>sub_code</th>
</tr>
</thead>
<tbody>
@foreach($results as $result)
<tr>
<td>{{$result->id}}</td>
<td>{{$result->lemma_id}}</td>
<td>{{$result->headword}}</td>
<td>{{$result->lemma}}</td>
<td>{{$result->pos}}</td>
<td>{{$result->gender}}</td>
<td>{{$result->language}}</td>
<td>{{$result->origin_family}}</td>
<td>{{$result->origin}}</td>
<td>{{$result->short_path}}</td>
<td>{{$result->origin_path}}</td>
<td>{{$result->etymology_text}}</td>
<td>{{$result->first_use}}</td>
<td>{{$result->lang}}</td>
<td>{{$result->pageid}}</td>
<td>{{$result->term}}</td>
<td>{{$result->non_latin_script}}</td>
<td>{{$result->lang_2}}</td>
<td>{{$result->gloss}}</td>
<td>{{$result->sequence}}</td>
<td>{{$result->lemma_id_fk}}</td>
<td>{{$result->derivatives_id}}</td>
<td>{{$result->etymology_id}}</td>
<td>{{$result->meaning}}</td>
<td>{{$result->semantic_category}}</td>
<td>{{$result->semantic_field}}</td>
<td>{{$result->simplicity_score}}</td>
<td>{{$result->age_score}}</td>
<td>{{$result->borrowed_score}}</td>
<td>{{$result->description}}</td>
<td>{{$result->typical_context}}</td>
<td>{{$result->representation}}</td>
<td>{{$result->sub_code}}</td>
</tr>
@endforeach
</tbody>
</table>
==编辑#2 ==
在这里做了一些其他测试并意识到使用时
$results = DB::table('lemma')
->join('etymology', 'lemma.id', '=', 'etymology.lemma_id_fk')
->join('gloss', 'etymology.id', '=', 'gloss.etymology_id')
->join('wold_meanings', 'gloss.gloss', '=', 'wold_meanings.meaning')
->select(DB::raw('lemma.*'))
->where('gloss.gloss', 'like', '%flower%')
->get();
它只给出了表格引理的结果,即它忽略了连接。因此,我会在<td>{{$result->id}}</td>
到<td>{{$result->pageid}}</td>
之间的视图表中收到结果,但不会显示其他结果。
答案 0 :(得分:1)
从链中删除对select()
的调用将使其正常工作。
select
方法代表查询的SELECT
部分。这样,写下以下内容:
->select(DB::raw('lemma.*'))
它与:
相同SELECT lemma.* FROM ...
换句话说,您只需从lemma
表中选择列。
但是,由于您还要从其他表中选择列,因此省略链的那一部分将使查询生成器回退到其默认行为,这与以下内容相同:
SELECT * FROM ...
如果你想明确你选择的是什么,这样做会达到同样的目的:
->select('*')