简体   繁体   English

MySQL中的子查询与PHP

[英]Subquery in mysql with php

Okay! 好的! Now I have a table where I am finding nearby lat lng with some formulas, now I wants to have the records having similar birthday month, then how can I achieve it. 现在我有一张桌子,可以在其中找到附近的经纬度公式,现在我想让记录具有相似的生日月份,那么我该如何实现。 I know I have to write sub-queries when I am trying I am getting syntax error. 我知道当我遇到语法错误时必须写子查询。 Error: 错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '6371 * acos( cos( radians(23.052349) ) * cos( radians( latitude ) ) * cos( rad' at line 1

This is my database structure 这是我的数据库结构

id | first_name | last_name |latitude | longitude | birthday
------------------------------------------------------------
1  | Jaymin     | Noob      | 23.0987 | 71.9876   | 1999-05-19
-------------------------------------------------------------
2  | Jaymin     | Noob      | 23.0942 | 74.9976   | 1996-05-19

Now this are my 2 queries 现在这是我的两个查询

SELECT id, ( 6371 * acos( cos( radians(23.052349) ) * cos( radians( latitude ) ) * 
cos( radians( longitude ) - radians(72.526460) ) + sin( radians(23.052349) ) * 
sin( radians( latitude ) ) ) ) AS distance FROM users HAVING
distance < 1 ORDER BY distance LIMIT 0 , 20;

To find nearby users using latitude and longitude 使用经度和纬度查找附近的用户

Below is my query to find having records with similar month. 以下是我的查询,以查找具有相似月份的记录。

 SELECT * FROM users WHERE MONTH(birthday) = '5'

Can anyone help me for subqueries 谁能帮我进行子查询

SELECT *, ( SELECT * FROM users WHERE MONTH(birthday) = '5',6371 * acos( cos( radians(23.052349) ) * cos( radians( latitude ) ) * 
cos( radians( longitude ) - radians(72.526460) ) + sin( radians(23.052349) ) * 
sin( radians( latitude ) ) ) ) AS distance FROM users HAVING
distance < 2 ORDER BY distance LIMIT 0 , 20;

I am trying this and getting error. 我正在尝试此操作并遇到错误。

I wanted to find only those users who are in same month and are nearby together. 我只想查找那些在同一个月内并在一起的用户。

If you put a subquery into the SELECT list, it need to return only 1 value -- it can't match multiple rows, and it has to select just a single column. 如果将子查询放到SELECT列表中,则它只需要返回1个值-它不能匹配多行,并且只能选择一个列。 You also need to match the parentheses around it. 您还需要匹配括号。

You don't need a subquery, just add WHERE MONTH(birthday) = 5 to the original query. 您不需要子查询,只需将WHERE MONTH(birthday) = 5到原始查询中。

SELECT id, 
    ( 6371 * acos( cos( radians(23.052349) ) * cos( radians( latitude ) ) * 
        cos( radians( longitude ) - radians(72.526460) ) + sin( radians(23.052349) ) * 
        sin( radians( latitude ) ) ) ) AS distance 
FROM users 
WHERE MONTH(birthday) = 5
HAVING distance < 1 
ORDER BY distance 
LIMIT 0 , 20;

This will find the users within 1 mile that have birthdays in May. 这将发现1英里以内的5月有生日的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM