以下是查询:
select a.*
from customers a INNER JOIN
(SELECT city,M IN(id) as id
FROM customers
GROUP BY city
) b
ON a.city = b.city AND a.id = b.id;
我有一个不时动态的列表。 但为了更好地理解,我正在提供样本。
order_field = ['check-in', 'check-out ASC',.....]
此order_field的设置取决于用户从UI中选择逐个选项。 在这里,我将所有传入的字段与我的列名称映射到数据库中。
Note: ASC for ascending. This could be like DESC also
然后我如何通过动态拟合order_field上的字段的结果获得订单。
有任何想法吗?
答案 0 :(得分:0)
试试这个。我在sql声明了order字段。
DECLARE @order_field varchar(15)
SET @order_field = 'DESC '
a.*
from customers a INNER JOIN
(SELECT city,M IN(id) as id
FROM customers
GROUP BY city
) b
ON a.city = b.city AND a.id = b.id;
ORDER BY CASE WHEN @order_field ='ASC' THEN a.city END,
CASE WHEN @order_field ='DESC' THEN a.city END DESC