簡體   English   中英

重復函數限制PostgreSQL

[英]Repeat function limitation postgresql

我的查詢如下:

SELECT id, repeat('*', integer) as experience
FROM table1

結果是:

id | experience

1  | **
2  | ***
3  | *******
4  | *
5  | ********

如您所見,id 3和5確實有7星和8星。 我希望我的查詢最多創建5星。 有沒有辦法限制重復功能?

通過應用minimum()函數,使用兩個值中的較小者:

SELECT id, repeat('*', least(5, some_colum)) as experience
FROM table1

(這假設some_column包含要使用的值)

SELECT id
      ,repeat('*', (case when col_name >=5 then 5 else col_name end)) as experience
FROM table1

暫無
暫無

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

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