简体   繁体   中英

Get custom MYSQL query result

SELECT
  route_locations.id
FROM points JOIN route_locations ON route_locations.location_id = points.location_id
WHERE points.id in (199, 205) 
group by route_locations.route_id

after running this query I am getting two value below:

id
1487
1491

first one is for points id 199 and second id is for points id 205.

Now as per my rule if first one is bigger than second one it will return 'HT' else return 'RT' how can I do that? if not possible how can I get the result like this?

point_id    route_location_id    
199           1487
205           1491

Use a self-join to get the two results separately, then compare them.

SELECT IF(l1.id > l2.id, 'HT', 'RT')
FROM points as p1
JOIN route_locations AS l1 ON p1.location_id = l1.location_id
CROSS JOIN points AS p2
JOIN route_locations AS l2 ON p2.location_id = l2.location_id
WHERE p1.id = 199
AND p2.id = 205

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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