How to set a default row for a query that returns no rows?

后端 未结 11 1445
青春惊慌失措
青春惊慌失措 2020-12-03 06:43

I need to know how to return a default row if no rows exist in a table. What would be the best way to do this? I\'m only returning a single column from this particular table

11条回答
  •  攒了一身酷
    2020-12-03 07:15

    *SQL solution

    Suppose you have a review table which has primary key "id".

    SELECT * FROM review WHERE id = 1555
    UNION ALL
    SELECT * FROM review WHERE NOT EXISTS ( SELECT * FROM review where id = 1555 ) AND id = 1
    

    if table doesn't have review with 1555 id then this query will provide a review of id 1.

提交回复
热议问题