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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -