• 示例:在查询中使用带搜索条件的 CASE 表达式
    1. Query OK, 0 rows affected (0.02 sec)
    2. obclient> CREATE TABLE t_case2(id number NOT NULL PRIMARY KEY, c_date date );
    3. Query OK, 0 rows affected (0.14 sec)
    4. obclient> INSERT INTO t_case2(id,c_date)
    5. VALUES (1,'2019-03-01')
    6. ,(2,'2019-05-08')
    7. ,(3,'2019-07-07')
    8. ,(4,'2019-10-11')
    9. Query OK, 6 rows affected (0.01 sec)
    10. Records: 6 Duplicates: 0 Warnings: 0
    11. obclient>
    12. obclient> SELECT id, c_date,
    13. CASE
    14. WHEN datediff(now(), c_date) > 12*30 THEN 'More than one year ago'
    15. WHEN datediff(now(), c_date) > 9*30 THEN 'More than three quarters ago'
    16. WHEN datediff(now(), c_date) > 6*30 THEN 'More than half a year ago'
    17. WHEN datediff(now(), c_date) > 3*30 THEN 'More than a quarter ago'
    18. WHEN datediff(now(), c_date) >= 0 THEN 'Within a quarter'
    19. END "Duration"
    20. FROM t_case2;
    21. +----+------------+------------------------------+
    22. | id | c_date | Duration |
    23. +----+------------+------------------------------+
    24. | 1 | 2019-03-01 | More than one year ago |
    25. | 2 | 2019-05-08 | More than three quarters ago |
    26. | 3 | 2019-07-07 | More than three quarters ago |
    27. | 4 | 2019-10-11 | More than a quarter ago |
    28. | 5 | 2019-12-12 | More than a quarter ago |
    29. | 6 | 2020-01-05 | Within a quarter |