您好,我有一个查询,我想要选择两个字段之一的值,取决于一个是空的.
field1 and field2
我想选择它们作为complete_field
IF field1 is empty,then complete_field is field2 ELSE complete_field is field1
在PHP中会像:
$complete_field = $field1 == '' ? $field2 : $field1;
在Postgresql中怎么做?
我试过了:
SELECT (IF field1 = '' THEN field2 ELSE field1) AS complete_field FROM table
但它没有工作.
请帮我:)谢谢
使用CASE WHEN .. THEN .. ELSE .. END,例如:
原文链接:https://www.f2er.com/postgresql/191737.htmlSELECT (CASE WHEN (field1 IS NULL OR field1 = '') THEN field2 ELSE field1 END) FROM table;