Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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 首先匹配like查询mysql laravel-5中的精确数据,然后匹配相关数据_Php_Mysql_Laravel 5 - Fatal编程技术网

Php 首先匹配like查询mysql laravel-5中的精确数据,然后匹配相关数据

Php 首先匹配like查询mysql laravel-5中的精确数据,然后匹配相关数据,php,mysql,laravel-5,Php,Mysql,Laravel 5,我已经检查了解决方案的链接,但找不到适合我的链接 我试图检索的记录,其中有确切的匹配第一,然后相关的匹配 以下是我到目前为止所做的尝试 $facility = DB::table('facility') ->select('ID', 'Name', 'Address', 'Phone', 'Latitude', 'Longitude') ->where('Name', 'like', $q . '%') ->where('Name', '!=', '')

我已经检查了解决方案的链接,但找不到适合我的链接

我试图检索的记录,其中有确切的匹配第一,然后相关的匹配

以下是我到目前为止所做的尝试

$facility = DB::table('facility')
    ->select('ID', 'Name', 'Address', 'Phone', 'Latitude', 'Longitude')
    ->where('Name', 'like', $q . '%')
    ->where('Name', '!=', '')
    ->limit(10)
    ->get();
if(empty($facility)) {
    $facility1 = DB::table('facility')
    ->select('ID', 'Name', 'Address', 'Phone', 'Latitude', 'Longitude')
    ->where('Name', 'like', '%' . $q . '%')
    ->where('Name', '!=', '')
    ->limit(10)
    ->get();
}
$facility->{'facility1'} = $facility1;
我尝试合并这两个对象。但是我没有得到想要的输出

有没有办法只通过一个查询获得输出


编辑:也就是说,我只想先检索完全匹配的记录,然后检索其他记录。例如,如果我搜索“OOS”,那么第一个记录应该是“OOS Healthcare”,然后是“Fat loss”。我想做一个查询,检查两个喜欢,然后选择一个伪列作为第一个“喜欢”是否匹配的结果,如果为false,则可以假设第二个匹配。然后简单地按伪列排序

在原始MySQL中,我认为这应该是:

SELECT ID, Name, Address, Phone, Latitude, Longitude,
IF(name = '$q',2,IF(name LIKE '$q%',1,0)) as `MatchStrength`
FROM facility
WHERE name like '%$q%' AND name != ''
ORDER BY MatchStrength DESC LIMIT 10
也可以在不使用额外列的情况下执行此操作:

SELECT ID, Name, Address, Phone, Latitude, Longitude
FROM facility
WHERE name like '%$q%' AND name != ''
ORDER BY IF(name = '$q',2,IF(name LIKE '$q%',1,0)) DESC LIMIT 10
将其转换为ORM逻辑是另一个挑战。可以尝试使用
DB::select作为原始SQL运行

它很难看,而且可能容易受到SQL注入的攻击,但请尝试一下,看看它是否能帮助您:

$facility = DB::table('facility')
    ->select('ID', 'Name', 'Address', 'Phone', 'Latitude', 'Longitude')
    ->where('Name', 'like', "%{$q}%")
    ->where('Name', '!=', '')
    ->orderByRaw("IF(name = '{$q}',2,IF(name LIKE '{$q}%',1,0)) DESC")
    ->limit(10)
    ->get();
通过使用
CONCAT

在MySQL中进行连接,可能可以切换到准备好的查询

我能够在《拉威尔》中用
orderByRaw
雄辩的表达方式使用它

$facility1 = DB::table('facility')
->select('ID', 'Name', 'Address', 'Phone', 'Latitude', 'Longitude')
->where('Name', 'like', '%' . $q . '%')
->where('Name', '!=', '')
->orderByRaw("(Name = '{$q}') desc, length(Name)")
->limit(10)
->get();
这将得到所有的条款,就像你所追求的,与确切的匹配在顶部。如果没有完全匹配,它将返回前10名

以下是我的应用程序的摘录:

$term = Term::where('title', 'LIKE', "%" . strtolower($query) . "%")
           ->orderByRaw("(title = '{$query}') desc, length(title)")
           ->first();

如果(空($facility)){
?@C2486我只想先检索与之完全匹配的记录,然后检索其他记录。就像我搜索“OOS”,那么第一个记录应该是“OOS Health care”,然后是“Fat loss”等等。@KeyurK我更新了代码。我想这里的键是一个
MatchStrength
列,你可以根据它排序。