MySQL averaging joined table

prgDevelop

I have two tables:

salon_ranks:

+-----------+-----------+-----------+-----------+
| salon_id  |category_id|  user_id  |    rank   |
+-----------+-----------+-----------+-----------+
|        10 |         1 |       999 |       1   |
|        10 |         2 |       999 |       1   |
|        10 |         1 |       888 |       5   |
|        10 |         2 |       888 |       5   |
+-----------+-----------+-----------+-----------+

categories:

+------+-----------+
|  id  |    name   |
+------+-----------+
|   1  |   prices  |
|   2  |   service |
+------+-----------+

I'm trying to efficiently get something that looks like this:

+-----------+-----------+-----------+
| salon_id  |category_id| AVG(rank) |
+-----------+-----------+-----------+
|        10 |         1 |       3   |
|        10 |         2 |       3   |
+-----------+-----------+-----------+

What made most sense to me is a query like that:

SELECT salon_id, category_id, AVG(rank)
FROM salon_ranks
INNER JOIN categories ON category_id = id
GROUP BY salon_id, category_id

But for some reason I don't get the average, but get this instead:

+-----------+-----------+-----------+
| salon_id  |category_id| AVG(rank) |
+-----------+-----------+-----------+
|        10 |         1 |       1   |
|        10 |         2 |       5   |
+-----------+-----------+-----------+

Any idea for why it would happen? And what might be the correct query?


I'm sorry, it was my mistake. I got the data from my original tables wrong. Really sorry for misleading you :(

Robert

Now we know your query works fine. But it's good practice to use aliases for tables. It's not necessary for your case but it makes your query more readable and scalable.

SELECT SR.salon_id, SR.category_id, AVG(SR.rank)
FROM salon_ranks SR
INNER JOIN categories C ON SR.category_id = C.id
GROUP BY SR.salon_id, SR.category_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prune mysql table - averaging records

From Dev

MySQL count on joined table

From Dev

Give an Alias to a Joined Table MySQL

From Dev

Mysql ordering by records that exist in joined table first

From Dev

Laravel mysql select count of joined table

From Dev

Fetch Column names from a joined mysql table

From Dev

Mysql - Return max value from joined table

From Dev

join with count on a joined table with group clause in mysql

From Dev

MySQL select SUM when a table is joined

From Dev

Update a table inner joined with itself in MySQL

From Dev

Mysql Query data between date on joined table

From Dev

Averaging a pivot table column

From Dev

MySql limiting only first table from joined table

From Dev

Update a mysql table column with sum of joined table columns value?

From Dev

MySQL update query on a single table with value from joined table

From Dev

MySQL return Null = 0 on joined table with filter on one table

From Dev

Use Columns From One Left Joined Table In Second Left Joined MYSQL

From Dev

Trying to correct MySQL query to return desired result w/ joined table

From Dev

Most recent distinct record from a joined MySQL table

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

Order by the nearest date in the joined table with a where condition - MySQL

From Dev

Sorting mysql result by joined table's specic IDs

From Dev

How to retrieve min price via mysql query from a joined table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

MySQL Inner Join Get Last Item of Joined Table

From Dev

MySQL: Select records where joined table matches ALL values

From Dev

How to find single row data from joined table in mysql?

From Dev

how to grab the not equal row from joined table in mysql?

From Dev

Can't Count results from Joined Table mySQL

Related Related

  1. 1

    Prune mysql table - averaging records

  2. 2

    MySQL count on joined table

  3. 3

    Give an Alias to a Joined Table MySQL

  4. 4

    Mysql ordering by records that exist in joined table first

  5. 5

    Laravel mysql select count of joined table

  6. 6

    Fetch Column names from a joined mysql table

  7. 7

    Mysql - Return max value from joined table

  8. 8

    join with count on a joined table with group clause in mysql

  9. 9

    MySQL select SUM when a table is joined

  10. 10

    Update a table inner joined with itself in MySQL

  11. 11

    Mysql Query data between date on joined table

  12. 12

    Averaging a pivot table column

  13. 13

    MySql limiting only first table from joined table

  14. 14

    Update a mysql table column with sum of joined table columns value?

  15. 15

    MySQL update query on a single table with value from joined table

  16. 16

    MySQL return Null = 0 on joined table with filter on one table

  17. 17

    Use Columns From One Left Joined Table In Second Left Joined MYSQL

  18. 18

    Trying to correct MySQL query to return desired result w/ joined table

  19. 19

    Most recent distinct record from a joined MySQL table

  20. 20

    MySQL: Select records where joined table matches ALL values

  21. 21

    Order by the nearest date in the joined table with a where condition - MySQL

  22. 22

    Sorting mysql result by joined table's specic IDs

  23. 23

    How to retrieve min price via mysql query from a joined table

  24. 24

    MySQL select row with two matching joined rows from another table

  25. 25

    MySQL Inner Join Get Last Item of Joined Table

  26. 26

    MySQL: Select records where joined table matches ALL values

  27. 27

    How to find single row data from joined table in mysql?

  28. 28

    how to grab the not equal row from joined table in mysql?

  29. 29

    Can't Count results from Joined Table mySQL

HotTag

Archive