如何处理在 sql 中动态设置列名

问题描述 投票:0回答:0

我正在尝试使用 sql 对我的数据进行排序,但我要排序的列是动态的,我现在明白你不能在 sql 中使用动态列名。但我不确定解决方法是什么。这是我的查询:

`select pipe.*,
    prod.details ->> 'manufacturer'      make,
    prod.details ->> 'model'             model,
    prod.details ->> 'model_year'        model_year,
    prod.details ->> 'advertised_price'  price,
    (select first_name || ' ' || last_name from tbl_users where id=pipe.user_id) as user_name,
    (select first_name || ' ' || last_name from tbl_customers where id=lead.customer_id) as customer_name
    from tbl_pipeline pipe
     join tbl_products prod on pipe.product_id = prod.id
     join tbl_leads lead on pipe.lead_id = lead.id
    ORDER BY ${field} DESC`

我只是在学习 sql,但我找不到明确的答案,我可以解释一下它是如何工作的。问题是我的字段值..显然是正确的..但它怎么可能是动态的而不是动态设置的??

sql postgresql sorting dynamic sql-order-by
© www.soinside.com 2019 - 2024. All rights reserved.