如何在PostgreSql数据中SELECT的时候A字段如果内容空就返回B字段的内容?

0
PostgreSQL 数据库 9830 次浏览

   content表有一个body字段和clean_body字段,单独获取body很容易用,select body from content就可以了,最近业务需要扩展了一个clean_body字段,用来存放body清洗后的数据。现在业务逻辑就是在clean_body中内容为空时,自动到body中去获取数据。

   如果直接将两个字段的内容都读出(select body,clean_body from content)然后在代码中自己判断也是可以的,这样做总觉得怪怪的,如果在数据库一层就可以做到的话是最完美的!

请尽量让自己的答案能够对别人有帮助

5个答案

默认排序 按投票排序
0

This article has suggested to me many new ideas. I will embark on doing it. Hope you can continue to contribute your talents in this area. Thank you. placar uol

0

I felt very happy while reading this site. This was a really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot!
fireboy and watergirl
 

0

http://uqseo.com/so/%E4%BC%AA%E5%9F%BA%E7%AB%99%E4%BF%A1%E6%81%AF%E7%BE%A4%E5%8F%91%E5%99%A8%E5%A4%9A%E5%B0%91%E9%92%B1%28152x0203x2162%E7%94%B5%E8%96%87%E5%90%8C%E5%8F%B7%29

0

阿是的对方水电费

1

你用下面这个SQL语句试试看:

SELECT (CASE WHEN (clean_body IS NULL OR clean_body = '') THEN body ELSE clean_body END) as body FROM content;