复杂代码用户搜索查询 [英] Complex codeignitor user search query
本文介绍了复杂代码用户搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个表用户,个人资料和位置。这些表的结构是:
用户
pre> user_id |电子邮件|通过|激活| banned
个人资料
表
user_id |名称|生日|图片|性别| last_active
位置
表
user_id |城市|国家|纬度|经度
现在这里是搜索条件:
- 必须激活(1)才能在搜索结果中显示。
- 用户不能被禁止(0)。
- 搜索结果必须根据经度和纬度位于附近的位置。
- 搜索结果必须按照上次活动时间排序。
要显示搜索,我需要用户名,图片,城市和国家,年龄,性别和离线/在线状态。 p>
我有一个查询按位置排序:
SELECT
经纬度
SQRT(POW(69.1 *(纬度-52.58),2)+ POW(69.1 *( - 1.12-经度)* COS(纬度/ 57.3),2))AS距离
FROM
location
ORDER BY
distance
为这些条件构建一个代码检查器查询
解决方案
你可以尝试构建这个查询
select l.latitude,l.longitude,
SQRT(POW(69.1 *(l.latitude - 52.58),2)+
POW (69.1 *( - 1.12-l.longitude)* COS(l.latitude / 57.3),2))作为距离
从用户作为u
离开连接简档为p
。 user_id = u.id
left join location as l
on l.user_id = u.id
其中u.activated = 1和u.banned = 0
按距离desc排序
我建议您在mysql中使用外键 >
I have 3 tables user, profile and location. The structure of these tables are:
user
table
user_id | email | pass | activated | banned
profile
table
user_id | name | birthday | picture | gender | last_active
location
table
user_id | city | country | latitude | longitude
Now here are search conditions:
- User must be activated(1) to appear in search result.
- User must not be banned(0).
- Search results must be nearby location according to latitude and longitude.
- search result must order by last active.
To display a search I will need user name, picture, city and country, age, gender and offline/online status.
I have a query to order by location:
SELECT
latitude, longitude,
SQRT( POW( 69.1 * ( latitude - 52.58 ) , 2 ) + POW( 69.1 * ( - 1.12 - longitude ) * COS( latitude / 57.3 ) , 2 ) ) AS distance
FROM
location
ORDER BY
distance
Can anyone contribute to build a codeignitor query for these conditions?
解决方案
you could try build around this query
select l.latitude, l.longitude,
SQRT( POW( 69.1 * ( l.latitude - 52.58 ) , 2 ) +
POW( 69.1 * ( - 1.12 - l.longitude ) * COS( l.latitude / 57.3 ) , 2 ) ) as distance
from user as u
left join profile as p
on p.user_id = u.id
left join location as l
on l.user_id = u.id
where u.activated=1 and u.banned=0
order by distance desc
May I suggest you lookup using foreign key constraints in mysql
这篇关于复杂代码用户搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文