Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.93 KB | None | 0 0
  1. /*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'.
  2.  
  3. QUERY:
  4. ----------------------------------------------------------------------*/
  5. SELECT
  6.     posts.id,  
  7.     posts.title,
  8.     posts.time,
  9.     (3959
  10.         * acos(cos(radians(?))
  11.         * cos(radians(posts.latitude))
  12.         * cos(radians(posts.longitude)
  13.         - radians(?))
  14.         + sin(radians(?))
  15.         * sin(radians(posts.latitude))))
  16.     AS distance,
  17.         SUM(votes.value) AS votes,
  18.     MAX(CASE WHEN votes.user_id = ? THEN value END) AS uservote
  19.         FROM posts
  20.     LEFT JOIN votes
  21.         on posts.id = votes.post_id
  22.     GROUP BY posts.id
  23.     HAVING distance < ?
  24.     ORDER BY time DESC /*IF I REMOVE THE 'time DESC' I get the correct result.*/
  25.     LIMIT 0, 20
  26.  
  27. /*RESULT:
  28. ----------------------------------------------------------------------
  29. [
  30.     {
  31.         "id": 127,
  32.         "title": "Working Hard",
  33.         "time": "2015-08-27 23:09:59",
  34.         "distance": 0.017432013625718,
  35.         "votes": null,
  36.         "uservote": null
  37.     },
  38.     {
  39.         "id": 125,
  40.         "title": "Vacation life is good!",
  41.         "time": "2015-08-15 23:44:03",
  42.         "distance": 126.02295676821, /*THIS DATA SHOULD'T BE PULLED CAUSE THE RADIUS VALUE IS LESS THEN 20*/
  43.         "votes": null,
  44.         "uservote": null
  45.     },
  46.     {
  47.         "id": 119,
  48.         "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. ",
  49.         "time": "2015-08-06 19:27:51",
  50.         "distance": 0.02953717270929,
  51.         "votes": "2",
  52.         "uservote": null
  53.     }
  54. ]
  55. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement