簡體   English   中英

如何根據另一個列的值更新選擇語句中列的值?

[英]How to update the values of a column in a select statement based on the values of anothr column?

我有兩個表:客戶和產品如下

客戶表

--------------------
|customer_id | rank|
|------------|-----|
|   001      |  0  | 
|   002      |  1  |

產品表

----------------------------------
|customer_id | country|  product |
|------------|--------|----------|
|   001      |  USA   |  Shoe    |
|   002      | FRANCE | Clothing |

我想要做的是列出產品表(所有列),如果等級為 0,則將 customer_id 值替換為“First”,如果等級為 1,則替換為“Free”。

我只是不知道該怎么做。 提前感謝您的幫助

考慮一個join和一個case表達式:

select p.*,
    case when c.rank = 0 then 'First'
         when c.rank = 1 then 'Free'
         else '??'
    end as res
from products p
inner join customers c on c.customer_id = p.customer_id

如果客戶的等級既不是0也不是1你就沒有告訴你想要什么。 查詢產生?? 在這種情況下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM