Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*When I use the following code I get extra result that shouldn't be there, please see the data result section. if you pay attention I get one longer distance that is not less radius user radius selection, that row shouldn't be pulled. However, if I remove 'ORDER BY time DESC' from the query, I get the correct result, but I need the 'ORDER BY time DESC'.
- QUERY:
- ----------------------------------------------------------------------*/
- SELECT
- posts.id,
- posts.title,
- posts.time,
- (3959
- * acos(cos(radians(?))
- * cos(radians(posts.latitude))
- * cos(radians(posts.longitude)
- - radians(?))
- + sin(radians(?))
- * sin(radians(posts.latitude))))
- AS distance,
- SUM(votes.value) AS votes,
- MAX(CASE WHEN votes.user_id = ? THEN value END) AS uservote
- FROM posts
- LEFT JOIN votes
- on posts.id = votes.post_id
- GROUP BY posts.id
- HAVING distance < ?
- ORDER BY time DESC /*IF I REMOVE THE 'time DESC' I get the correct result.*/
- LIMIT 0, 20
- /*RESULT:
- ----------------------------------------------------------------------
- [
- {
- "id": 127,
- "title": "Working Hard",
- "time": "2015-08-27 23:09:59",
- "distance": 0.017432013625718,
- "votes": null,
- "uservote": null
- },
- {
- "id": 125,
- "title": "Vacation life is good!",
- "time": "2015-08-15 23:44:03",
- "distance": 126.02295676821, /*THIS DATA SHOULD'T BE PULLED CAUSE THE RADIUS VALUE IS LESS THEN 20*/
- "votes": null,
- "uservote": null
- },
- {
- "id": 119,
- "title": "Pellentesque finibus arcu velit, et sollicitudin elit lacinia ut. Nulla ut malesuada massa. Fusce iaculis elit nibh, ac volutpat magna placerat vitae. Donec diam orci, venenatis at sapien sed posuere. ",
- "time": "2015-08-06 19:27:51",
- "distance": 0.02953717270929,
- "votes": "2",
- "uservote": null
- }
- ]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement