当我们使用Raw SQL时,如何在Laravel中使用like子句?

时间:2016-06-30 15:55:29

标签: php mysql laravel eloquent

我使用的是Laravel原始sql查询功能。并且必须执行类似的查询。这段代码有很多连接和检查。因此,使用原始SQL会更好。一切都很好。但是当我使用它时,会出现错误。

$Stars = DB::select('select v.videoid,s.seriesshortname, v.videotitle, v.VideoShortName, v.VideoImagepath, v.Views, v.Likes, v.Dislikes, v.Rating, v.videocategory, v.lastupdated,v.videocategory,v.seriesid,v.genreid,v.studioid from tblvideo v, tblpornstarvideo pv,tblseries s,tblpornstar p where v.videoid = pv.videoid and v.seriesid = s.seriesid and upper(v.Active) = \'Y\' and pv.psid = p.psid and pv.psid = :id and (v.site = 1 or v.site=3) and v.videotitle like \':letter%\' order by v.videotitle limit 6 offset :offset', ['id' => $id, 'offset' => $offset]);

请检查相似的代码。 (v.videotitle喜欢\':字母%\')

请告诉我如何使其工作。 我按照这里的文档。 https://laravel.com/docs/5.1/database

1 个答案:

答案 0 :(得分:2)

将通配符添加到变量中,而不是查询中,并且不添加引号。你也没有传递字母变量:

$Stars = DB::select("select v.videoid,s.seriesshortname, v.videotitle, v.VideoShortName, v.VideoImagepath, v.Views,
v.Likes, v.Dislikes, v.Rating, v.videocategory, v.lastupdated,v.videocategory,v.seriesid,v.genreid,v.studioid
from tblvideo v, tblpornstarvideo pv,tblseries s,tblpornstar p
where v.videoid = pv.videoid and v.seriesid = s.seriesid and
upper(v.Active) = 'Y' and pv.psid = p.psid and pv.psid = :id and (v.site = 1 or v.site=3)
and v.videotitle like :letter order by v.videotitle limit 6 offset :offset", ['id' => $id, 'letter'=> $letter.'%', 'offset' => $offset]);