我想尝试使用以下内容而不确定如何解决这个问题
SELECT * FROM search_users
WHERE
match(first_name,last_name,country,city,location,nationality,short_bio)
against (?)
AND
search_users.loc_lng BETWEEN '-0.24272918701172' AND '-0.24272918701172'
AND
search_users.loc_lat BETWEEN '51.47026338272' AND '51.47026338272'
我正在尝试编写一个与
完全相同的laravel
查询
select * from search_users
where
......
and search_users.loc_lng BETWEEN '-0.24272918701172' AND '-0.24272918701172'
AND search_users.loc_lat BETWEEN '51.47026338272' AND '51.47026338272'
答案 0 :(得分:21)
如果您只想构建查询/获取纯数据而没有任何逻辑,您只需使用查询生成器:
$results = DB::table('search_users')
->where('first_name', $firstname)
->where('last_name', $last_name)
->where('country', $country) //and so on
->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172))
->whereBetween('loc_lat', array(51.47026338272, 51.47026338272))
->get();
如果您使用的是模型,确实可以使用相同的语法:
$users = User::where('key1', $value1)
->where('key2', $value2)
->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172))
->whereBetween('loc_lat', array(51.47026338272, 51.47026338272))
->get();
关于如何在eloquent中使用AND
的问题的一些额外解释:
AND
,则默认使用 where()
。所以
DB::table('search_users')->where('first_name', $firstname)
->where('last_name', $last_name)
等于
SELECT * FROM search_users WHERE first_name = ? AND last_name = ?
对于OR
,您可以使用orWhere()
:
DB::table('search_users')->where('first_name', $firstname)
->orWhere('last_name', $othername)
等于
SELECT * FROM search_users WHERE first_name = ? OR first_name = ?
有时您可能需要更复杂的东西,例如:
SELECT * FROM search_users
WHERE first_name = ?
AND (last_name = ? OR last_name = ?)
AND age > 27
在Eloquent中,这将是:
DB::table('search_users')
->where('first_name', $firstname)
->where(function($query) {
$query->where('last_name', $lastName1);
$query->orWhere('last_name', $lastName2);
})
->where('age', '>', 27)