Oracle中的自定义排序无效

时间:2016-12-22 10:20:35

标签: oracle oracle11g

我正在处理以下查询

Select name from (Select 'Mana' as name from table1
union 
Select 'Tom' as name from dual)A
order by case 
when name = 'TOM' then '1'
else name 
end ;

给出输出

 Mana
 Tom

和自定义排序后我想要的结果是

Tom
Mana
  

请不要回答订单由desc,因为我有其他值   专栏,我需要自定义排序

感谢

3 个答案:

答案 0 :(得分:4)

您正在将name'TOM'进行比较,但该列中的值为'Tom' - 案例不匹配。

Select name from (Select 'Mana' as name from dual
union 
Select 'Tom' as name from dual)A
order by case 
when name = 'Tom' then '1'
else name 
end ;

NAME
----
Tom
Mana

您可能更喜欢添加标记以进行排序,然后按名称进一步排序:

Select name from (Select 'Mana' as name from dual
union 
Select 'Tom' as name from dual)A
order by case when name = 'Tom' then 1 else 2 end,
name;

NAME
----
Tom
Mana

这也避免了角色集的任何潜在问题 - 你假设角色' 1'总是排在' T'之前。 (这可能是真的,但仍然......)

答案 1 :(得分:1)

这是你需要的吗?

    function listProducts() {
    $('#shopProducts').empty();

    $.ajax({
        method: "GET",
        url: kinveyBaseUrl + "appdata/" + kinveyAppKey + "/products",
        headers: getKinveyUserAuthHeaders(),
        success: loadProductsSuccess,
        error: handleAjaxError
    });
    // Load Products Success
    function loadProductsSuccess(products) {
        let table = $(`
                    <table>
                        <tr>
                            <th>Product</th>
                            <th>Description</th>
                            <th>Price</th>
                            <th>Actions</th>
                        </tr>
                    </table>`);
        let tr = $('<tr>');
        for (let product of products) {
            let tr = $('<tr>');
            displayTableRow(tr, product);
            tr.appendTo(table);
        }
        tr.appendTo(table);
        $('#shopProducts').append(table);
    }
    function displayTableRow(tr, product) {
        let links = [];
        let purchaseLink;
        purchaseLink = $("<button>Purchase</button>").click(function () {
            let currentPurchaseItem = $.ajax({
                method: "GET",
                url: kinveyBaseUrl + "appdata/" + kinveyAppKey + "/products/" + this.id,
                headers: getKinveyUserAuthHeaders(),
                error: handleAjaxError
            });
            //showMenuCartView();
            //listMyCartProducts();
            console.dir(currentPurchaseItem);
            console.dir(this.id);
        });
        links.push(purchaseLink);
        tr.append(
            $("<td>").text(product.name),
            $("<td>").text(product.description),
            $("<td>").text(product.price),
            $("<td>").append(links)
        );
    }
}

在您的order by case when name = 'TOM' then 1 else 2 end ; 条款中,order bynumbervarchar而导致case请求将varchar请求使用相同的类型。

Select name from 
  (
  Select 'Mana' as name from dual
  union all
  Select 'Tom' as name from dual
  ) A
order by 
  case 
    when upper(name) = 'TOM' then 1
    else 2
  end;

请注意upper / lower区分大小写

答案 2 :(得分:1)

您可以执行以下操作:

order by case 
when upper(name) = 'TOM' then 1
else 2
end, name ;

这将使TOM保持在最顶层,然后休息