2016-04-28 18 views
0

に動的で注文する方法以下のクエリです:Pythonは:RawQueryセット

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は、ユーザーがUIからオプションで並べ替えを選択するかどうかによって設定されます。 ここでは、すべての着信フィールドをDBの列名でマッピングしています。

Note: ASC for ascending. This could be like DESC also 

次に、order_fieldのフィールドに動的にフィットする結果でどのように注文を取得できますか?

誰でも知っていますか?

答えて

0

これを試してください。私はSQLで注文フィールドを宣言します。

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 
関連する問題