php - MySQL Prevent sum twice -
i have following query in php script:
select sum(score) + 1200 rating users_ratings user_fk = ?
the problem if user has no rows in table, rating returned null
. added if case:
select if(sum(score) null, 0, sum(score)) + 1200 rating users_ratings user_fk = ?
but i'm wondering how query, without repeating sum(score)
. i'm guessing if have lot of rows, sum repeat twice, affect performance of application.
here simpler method:
select ( coalesce(sum(score), 0) + 1200 ) rating users_ratings user_fk = ?;
coalesce()
ansi standard function returns first non-null
value list of expressions.
however, additional overhead of calculating sum(score)
twice -- if happens -- should minimal compared rest of query (finding data, reading in, summarizing 1 row).
Comments
Post a Comment