我想对查询结果进行排序,以便 2 列按升序排序。两列都可以包含值 0 或 NULL,然后在排序时忽略这些值。
我不知道我是否表达清楚了。
这是我的询问:
SELECT tripad_order, tourad_order
FROM(
SELECT tripad_order, tourad_order
FROM tripdetails
LEFT JOIN tripaddresses ON tripaddresses.trip_id = tripdetails.trip_id
LEFT JOIN touraddresses ON touraddresses.tripad_id = tripaddresses.tripad_id AND touraddresses.tour_id = tripdetails.tour_id
WHERE tripde_id = 39780
UNION ALL
SELECT tripad_order, tourad_order
FROM tripdetails
LEFT JOIN touraddresses ON touraddresses.tour_id = tripdetails.tour_id
LEFT JOIN tripaddresses ON tripaddresses.tripad_id = touraddresses.tripad_id AND tripaddresses.trip_id IN (0,tripdetails.trip_id)
WHERE tripde_id = 39780 AND tripad_type IS NOT NULL
) t
GROUP BY tripad_id
ORDER BY tourad_order
这是结果
tripad_订单 | tourad_order |
---|---|
2 | 空 |
0 | 1 |
1 | 2 |
3 | 4 |
0 | 5 |
这是想要的:结果 tripad_order 和tourad_order 都应该按升序排列,忽略0和NULL
tripad_订单 | tourad_order |
---|---|
0 | 1 |
1 | 2 |
2 | 空 |
3 | 4 |
0 | 5 |
SELECT tripad_order, tourad_order
FROM (
SELECT tripad_order, tourad_order
FROM tripdetails
LEFT JOIN tripaddresses ON tripaddresses.trip_id = tripdetails.trip_id
LEFT JOIN touraddresses ON touraddresses.tripad_id =
tripaddresses.tripad_id AND touraddresses.tour_id = tripdetails.tour_id
WHERE tripde_id = 39780
UNION ALL
SELECT tripad_order, tourad_order
FROM tripdetails
LEFT JOIN touraddresses ON touraddresses.tour_id = tripdetails.tour_id
LEFT JOIN tripaddresses ON tripaddresses.tripad_id =
touraddresses.tripad_id AND tripaddresses.trip_id IN (0,
tripdetails.trip_id)
WHERE tripde_id = 39780 AND tripad_type IS NOT NULL
) t
GROUP BY tripad_order, tourad_order
ORDER BY
CASE WHEN tripad_order = 0 THEN NULL ELSE tripad_order END,
CASE WHEN tourad_order IS NULL THEN 1 ELSE 0 END,
tourad_order;