Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.26 KB | None | 0 0
  1. -- This query should fetch the whole tours that need to be matched with an agent
  2. -- We should take the closest agent to the tour's apartment
  3. -- We should check that this agent doesn't have exists tour_request for this tour
  4. -- We should check that this agent doesn't have another tour for this time (agent can't be at 2 places at the same time)
  5.  
  6. SELECT
  7.     tours.id as tour_id,
  8.     users.id as matched_user_id,
  9.     ((ACOS(SIN(apartments.latitude * PI() / 180) * SIN(users.latitude * PI() / 180) + COS(apartments.latitude * PI() / 180) * COS(
  10.         users.latitude * PI() / 180) * COS((apartments.longitude - users.longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.853159616) as distance
  11. FROM
  12.     tours
  13. INNER JOIN
  14.     apartments
  15.         ON
  16.             tours.apartment_id = apartments.id
  17. INNER JOIN
  18.     users
  19. LEFT OUTER JOIN
  20.     tour_requests
  21.         ON
  22.             tour_requests.tour_id = tours.id AND
  23.             tour_requests.user_id = users.id
  24.  
  25. LEFT OUTER JOIN
  26.     tours as user_tours
  27.         ON
  28.             user_tours.user_id = users.id AND
  29.             user_tours.tour_at = tours.tour_at
  30. WHERE
  31.     tours.status = 'pending' AND
  32.     tours.user_id is NULL AND
  33.     tours.tour_at < DATE_ADD(NOW(), INTERVAL 90 MINUTE) AND
  34.     tours.tour_at > DATE_ADD(NOW(), INTERVAL 35 MINUTE) AND
  35.     tour_requests.id is NULL AND
  36.     user_tours.id is NULL
  37. ORDER BY distance
  38. LIMIT 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement