Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我们使用原始SQL时,如何在Laravel中使用like子句?_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

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

Php 当我们使用原始SQL时,如何在Laravel中使用like子句?,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我正在使用Laravel原始sql查询功能。并且必须执行类似的查询。这段代码中有太多的连接和检查。所以最好使用原始SQL。一切正常。但是当我使用like时,有一个错误 $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.

我正在使用Laravel原始sql查询功能。并且必须执行类似的查询。这段代码中有太多的连接和检查。所以最好使用原始SQL。一切正常。但是当我使用like时,有一个错误

$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.像\':字母%\'这样的视频标题)

请告诉我如何让它工作。 我遵循了这里的文档。
将通配符添加到变量中,而不是查询中,并且不要添加引号。您也没有传入字母变量:

$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]);

使用原始SQL并不更好。事实上更糟。它使PHP代码对任何可怜的灵魂来说都不那么可读,而这些灵魂将来可能会被要求维护PHP代码。就性能而言,这可能与使用fluent生成查询时相同。@apokryfos通常最好使用查询生成器,但有时这无法完成任务。我不确定这是不是真的,但这是一个学习的机会。@apokryfos有这么多的连接和条件。我还喜欢只使用标准的有说服力的查询。但是我试过了,结果太难了(很好。这解决了我的问题:)非常感谢,伙计:)