Skip to content
Advertisement

SELECT from table with count from another

I have 2 mysql tables

I need to get all views by user, but also need to get the user role (for each view) and calculate a dynamic number of users used for every view.

So, I need to get all view columns, plus the role of the user which do this request (from ViewUser), plus count of users for each view.

With this example, I lost the counter for another users, because I use condition userId = 33 at this level. I can do it in only a query, or I just need to use one extra more?

An example:

viewUser:

from this, I need to get 3 lines based on userId = 33 condition:

edit :

This query get the results how I need. But I’m wondering if exists another way, optimized to do that.

Advertisement

Answer

Try to use correlated subquery:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement