How can I get the average amount of columns in MySQL? [duplicate]












0
















This question already has an answer here:




  • calculate an avg with no decimals

    3 answers




I've a table in my database called rating:



id   user_id    rating

1 2 5
2 3 5
3 1 2
4 1 1
5 1 5
6 2 4


The point is that I don't know the exact amount of the entries. It can be 100 entries or just 4.
For example when I want to know the average rating for the user with user_id = 1 what should I do in the SQL statement?



I've a known parameter $user_id to get the rating of each user.



Let's say the average rating of user_id = 1 is 2.6 I've a problem because I need to round it to the next higher number which is 3.



So this is to complicated for me. I've tried this here but it's not working:



$total_rating = SELECT COUNT(*) FROM rating WHERE user_id = $user_id
$rating = what know?


I'm not that good in SQL so I would be very thankful if you can help me!










share|improve this question















marked as duplicate by Shadow mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 22:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    ceil(avg(rating)), not count(*).

    – Shawn
    Nov 23 '18 at 22:03













  • Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

    – Shidersz
    Nov 23 '18 at 22:04


















0
















This question already has an answer here:




  • calculate an avg with no decimals

    3 answers




I've a table in my database called rating:



id   user_id    rating

1 2 5
2 3 5
3 1 2
4 1 1
5 1 5
6 2 4


The point is that I don't know the exact amount of the entries. It can be 100 entries or just 4.
For example when I want to know the average rating for the user with user_id = 1 what should I do in the SQL statement?



I've a known parameter $user_id to get the rating of each user.



Let's say the average rating of user_id = 1 is 2.6 I've a problem because I need to round it to the next higher number which is 3.



So this is to complicated for me. I've tried this here but it's not working:



$total_rating = SELECT COUNT(*) FROM rating WHERE user_id = $user_id
$rating = what know?


I'm not that good in SQL so I would be very thankful if you can help me!










share|improve this question















marked as duplicate by Shadow mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 22:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    ceil(avg(rating)), not count(*).

    – Shawn
    Nov 23 '18 at 22:03













  • Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

    – Shidersz
    Nov 23 '18 at 22:04
















0












0








0









This question already has an answer here:




  • calculate an avg with no decimals

    3 answers




I've a table in my database called rating:



id   user_id    rating

1 2 5
2 3 5
3 1 2
4 1 1
5 1 5
6 2 4


The point is that I don't know the exact amount of the entries. It can be 100 entries or just 4.
For example when I want to know the average rating for the user with user_id = 1 what should I do in the SQL statement?



I've a known parameter $user_id to get the rating of each user.



Let's say the average rating of user_id = 1 is 2.6 I've a problem because I need to round it to the next higher number which is 3.



So this is to complicated for me. I've tried this here but it's not working:



$total_rating = SELECT COUNT(*) FROM rating WHERE user_id = $user_id
$rating = what know?


I'm not that good in SQL so I would be very thankful if you can help me!










share|improve this question

















This question already has an answer here:




  • calculate an avg with no decimals

    3 answers




I've a table in my database called rating:



id   user_id    rating

1 2 5
2 3 5
3 1 2
4 1 1
5 1 5
6 2 4


The point is that I don't know the exact amount of the entries. It can be 100 entries or just 4.
For example when I want to know the average rating for the user with user_id = 1 what should I do in the SQL statement?



I've a known parameter $user_id to get the rating of each user.



Let's say the average rating of user_id = 1 is 2.6 I've a problem because I need to round it to the next higher number which is 3.



So this is to complicated for me. I've tried this here but it's not working:



$total_rating = SELECT COUNT(*) FROM rating WHERE user_id = $user_id
$rating = what know?


I'm not that good in SQL so I would be very thankful if you can help me!





This question already has an answer here:




  • calculate an avg with no decimals

    3 answers








mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:11









Shadow

25.7k92844




25.7k92844










asked Nov 23 '18 at 21:58









Mr. JoMr. Jo

662114




662114




marked as duplicate by Shadow mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 22:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Shadow mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 22:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    ceil(avg(rating)), not count(*).

    – Shawn
    Nov 23 '18 at 22:03













  • Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

    – Shidersz
    Nov 23 '18 at 22:04
















  • 1





    ceil(avg(rating)), not count(*).

    – Shawn
    Nov 23 '18 at 22:03













  • Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

    – Shidersz
    Nov 23 '18 at 22:04










1




1





ceil(avg(rating)), not count(*).

– Shawn
Nov 23 '18 at 22:03







ceil(avg(rating)), not count(*).

– Shawn
Nov 23 '18 at 22:03















Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

– Shidersz
Nov 23 '18 at 22:04







Just use SELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id. That will give you what you need.

– Shidersz
Nov 23 '18 at 22:04














1 Answer
1






active

oldest

votes


















3














SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';


That should do it!






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    SELECT ROUND(AVG(rating)) AS 'Average Rating'
    FROM rating
    WHERE user_id = '$user_id';


    That should do it!






    share|improve this answer




























      3














      SELECT ROUND(AVG(rating)) AS 'Average Rating'
      FROM rating
      WHERE user_id = '$user_id';


      That should do it!






      share|improve this answer


























        3












        3








        3







        SELECT ROUND(AVG(rating)) AS 'Average Rating'
        FROM rating
        WHERE user_id = '$user_id';


        That should do it!






        share|improve this answer













        SELECT ROUND(AVG(rating)) AS 'Average Rating'
        FROM rating
        WHERE user_id = '$user_id';


        That should do it!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 22:07









        Nick DawesNick Dawes

        919




        919















            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...