I structured a PHP table with two columns: 1 for the hospital Id and 1 for the rating.
When a user rates the page, in the Id section goes a default id (depending on the page) and the rating goes in the rating section obv.
Now, how can I calculate the average rating only for a certain ID?
I've never studied Php so I need some help! Thanks everyone!!
You can use AVG on rating column and GROUP BY on hotel_id column.
Can refer below query.
SELECT hotel_id, avg(rating)
FROM hotel_rating
GROUP BY hotel_id;
PS: This is sample query you need to substitute your column names and table names in that
Related
Аctually I am working on the CI medical project. I have column name visit date. I want to display the total no of visits by each patient on that specific date, for example 5-4-2019 total visits by that patient were 3. Similarly 4-3-2020 total visits were 5.
How can I create another column that counts total visits based on this column visit date?
Since you have provided no DDL or DML, the best we can do is guess at what your schema might be and what your code might look like. So, here's a guess:
select -- <your existing columns>
visits_on_this_date = count(*) over (partition by patient_id, visit_date)
from -- the rest of your query here
Something simple like that?
select patient_id,visit_date,count(*) as total_visits
from table
group by patient_id,visit_date
I'm a begginer in MySQL and have been searching for a solution for this problem for a few hours now. I found a few answers, but can't seem to implement them.
Basically, heres what I want to do:
I want to find how a user from from a given country would rank compared to other users from the same country when they are ranked by ID
For example:
Joe is ID 10 and is from the US
There are 100 users from the US
Result: Joe ranks 10th
How would I convert this to an MySQL query?
Thank you in advance.
Create 2 user variables,eg,running & previous which gets the previous rank number and accordingly calculate the rank number of the country by incrementing the current value by 1.
select u.id,u.country,u.rank
from (
select u1.id,u1.country,
#running:=if(#previous=concat(u1.id,u1.country),#running,0) + 1 as rank,
#previous:=concat(u1.id,u1.country)
from tablename u1
order by concat(t.id,t.country)
) u;
Replace tablename with your table
I want to work out an average "rating" for my cocktails in a table. There is a tblRating linked to tblCocktail and within this table is "value" and "counter". "value" contains the 1-5 rating a user can give each cocktail, and "counter" contains the value "1". I want to work out the average rating for each cocktail by adding up all the "value"s and dividing it by the number of "counter"s
I've never done mathematical calculations based on php fields before so any help is appreciated
-matt
You probably do not need the counter field, the AVG function can calculate averages without requiring a counter:
SELECT AVG(value)
FROM tblRating
WHERE cocktail_id = 1234
Notes:
You can count the number of rows inside the rating table to determine the number of ratings
NULL values are not taken into account when average is calculated
You must stick with your chosen scale and not allow values outside the 1-5 range
Here's an exaple that should do what you want, obviusly without having the full table schema it is not possible being more accurate, but the example should show the idea :
SELECT c.name, AVG(r.value)
FROM tblCocktail c
JOIN tblRating r ON c.id = r.cocktail.id
GROUP BY c.id, c.name
Basically you are selecting all cocktail from tblCocktail and calculating the average of the rating(r.value).
I need to know how to handle a pretty complex situation.
I have a system that allows users to vote up or down on comments that others make. I want to create a report of those with the most up votes based on all of their comments. The upvotes were not tracked in the users table, only in the comments table so it needs to go through the comments table and get the value in the vote column and output the sum of all of the vote column values for each userid. It then needs to order these and output the top 10.
Thanks in advance for help
If you post your users and comments table structure I could make a query. But it would be something like this:
SELECT SUM(votes) total, user_id FROM comments GROUP BY user_id ORDER BY total LIMIT 10
for example i have a table like this :
name rating
matei 124
andrei 20
serj 25
john 190
mike 96
andy 245
tom 73
i need to output something like this(order by rating):
john's position is 2; or, tom's position is 5; (i don't need to get all result , just one )
How can I achieve this?
Thanks in advance
Generally order of rows in a query result is not guaranteed by MySQL unless ordering is explicitly specified with ORDER BY clause. If you have some separate ordering column, you may use query like the following:
SELECT count(1) as position
FROM table
WHERE order_column <= {john's order_column value};
If you don't have ordering column, I'd recommend you to define first, what does "john's position" and "tom's position" mean.
UPDATE:
AFAIU, you want to get position in list sorted by rating (sorry, I initially did not get it). So, rating would be your order_column. In this case, you should decide, how do you calculate position, if two guys have equal rating (who's position is higher?).
So, the query may look in the following way:
SELECT count(1) as position
FROM table
WHERE
rating > (SELECT rating FROM table WHERE id={user's ID});
SELECT COUNT(*) + 1
FROM users
WHERE (rating, name) <
(
SELECT rating, name
FROM users
WHERE name = 'john'
)
Note that if you will have duplicates on both name and rating, this query will assign the same rating to both of them.
Tables are more formally known as relations in database literature - they are not guaranteed to be ordered (they are sets of "tuples"), so your question doesn't make sense. If you need to rely on an order/position, you need to define an additional column (like an auto-incrementing ID column) to capture and store that info.
Is this any help > http://craftycodeblog.com/2010/09/13/rownum-simulation-with-mysql/ ?
Would offset not work like so?
SELECT * FROM Table ORDER BY rating DESC LIMIT 1,6
This would return 1 row that has been off setted by 6 rows ? or am I mistaken, the syntax would be
SELECT * FROM Table ORDER BY rating DESC LIMIT 1 , {{POS}}