MySQL user defined functions

前端 未结 1 868
时光取名叫无心
时光取名叫无心 2021-01-01 03:14

I have a table contains a few columns say: column_1, column_2 and column_3.

I appended an new column to the table called score. What I want to do is calculate the sc

相关标签:
1条回答
  • 2021-01-01 03:53

    Yes.

    CREATE FUNCTION `getScore`(`a` DECIMAL(12,4), `b` DECIMAL(12,4), `c` DECIMAL(12,4)) RETURNS DECIMAL(12,4)
    BEGIN
        RETURN a + b + c;
    END
    
    SELECT getScore(0.3, 0.4, 0.5)
    -> 1.2000
    

    But if you need some values from the table, you need to include those as parameters too.

    SELECT getScore(column1, column2, column3, 0.5, 0.1, 0.4) AS score FROM table
    
    0 讨论(0)
提交回复
热议问题