How can I get the average amount of columns in MySQL? [duplicate]
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!
mysql sql
marked as duplicate by Shadow
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.
add a comment |
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!
mysql sql
marked as duplicate by Shadow
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))
, notcount(*)
.
– Shawn
Nov 23 '18 at 22:03
Just useSELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id
. That will give you what you need.
– Shidersz
Nov 23 '18 at 22:04
add a comment |
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!
mysql sql
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
mysql sql
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
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
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))
, notcount(*)
.
– Shawn
Nov 23 '18 at 22:03
Just useSELECT CEIL(AVG(rating)) FROM rating WHERE user_id = $user_id
. That will give you what you need.
– Shidersz
Nov 23 '18 at 22:04
add a comment |
1
ceil(avg(rating))
, notcount(*)
.
– Shawn
Nov 23 '18 at 22:03
Just useSELECT 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
add a comment |
1 Answer
1
active
oldest
votes
SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';
That should do it!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';
That should do it!
add a comment |
SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';
That should do it!
add a comment |
SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';
That should do it!
SELECT ROUND(AVG(rating)) AS 'Average Rating'
FROM rating
WHERE user_id = '$user_id';
That should do it!
answered Nov 23 '18 at 22:07
Nick DawesNick Dawes
919
919
add a comment |
add a comment |
1
ceil(avg(rating))
, notcount(*)
.– 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