如何从Postgresql查询中检索包含“CASE WHEN”语句的数据?

时间:2011-10-19 08:22:36

标签: postgresql

我有这样的postresql查询:

SELECT * , u.image AS image_name, k.id AS cat_id,
   CASE WHEN s.web_site!="" THEN s.web_site
   ELSE 'Not available' END AS v_site
FROM users u
LEFT JOIN comments s ON s.user_id = u.id WHERE u.id = '1547'

查询结果:

users     | name    | v_site
----------+---------+---------------
ali       | hassan  | www.domain.com
turqut    | sandra  | www.somesite.com
emil      | azizov  | www.website.com

正如您所见,我的网站名称是 www.domain.com 。但我希望通过HTML超链接标记获得:< a href ='www.domain.com'> www.domain.com < / A>

我该怎么做?

1 个答案:

答案 0 :(得分:6)

!= ""是无效的SQL语法。字符串用单个qutoe分隔。

SELECT *, 
       u.image AS image_name, 
       k.id AS cat_id,
       CASE 
         WHEN s.web_site != '' THEN '<a href="'||s.web_site||'">'||s.web_site||'</a>'
         ELSE 'Not available' 
       END AS v_site
FROM users u
LEFT JOIN comments s ON s.user_id = u.id WHERE u.id = '1547'