Return Default value if no row found -mysql

后端 未结 4 1559
心在旅途
心在旅途 2020-12-20 18:57

I\'m building query to search multiple rows from database like

SELECT 
  * 
FROM
  table1 
WHERE col1 = \'012311\' 
  OR col1 = \'0123631\' 
  OR col1 = \'0         


        
4条回答
  •  死守一世寂寞
    2020-12-20 19:14

    There are several approaches, which depend on the dataset. Here's one way:

    SELECT *, 
    IFNULL(
        ( SELECT col1 FROM table1 
            WHERE col1 IN ('012311','0123631','091233','092111') 
        ),
        'some_value'
    ) AS my_col1
    FROM table1;
    

    Not neccessarily copy+paste, you will have to adjust for your specific case.

提交回复
热议问题