join count from one table to select from another - mysql

Kadosh

I have a select query with a subquery in it on table a:

select UserName, Name, anniversaryDate, RegionCode 
from a where UserName in
(
    select Username from
    (
        select Username, max(timeUsed)
        from log_table where role='Cou'
        group by Username order by max(timeUsed) desc limit 10
    ) a
)

which works fine, now I have another table called b, on that table I only have 3 columns:

UserName, PName and feedback

which have each UserName multiple times and I want to join to my first query the count of each UserName I got from the first query.

for eaxmple:

SELECT COUNT(UserName) FROM b where UserName='Admin' group by UserName

How do I join the results from the second query to the first one?

Tables:

log_table :
id - Int, AI, PK
Username - varchar
Action - Int
timeUsed - Datetime
role - varchat

a:
UserName - varchar, PK
Name - varchar
anniversaryDate - Datetime
RegionCode - Int

b:
UserName - varchar
PName - varchar
feedback - varchar

rekaszeru

Based on your table structure the query below might add the necessary column you are looking for:

    select users.UserName, Name, anniversaryDate, RegionCode, 
        Coalesce(count(b.UserName),0) as cnt 
    from users 
        left outer join (select Username, max(timeUsed)
            from log_table where role='Cou'
            group by Username order by max(timeUsed) desc limit 10
        ) a using (UserName)
        left outer join fedbacks b on (a.UserName = b.userName)
        group by users.UserName;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL join one colum from one table to two count() in another

From Dev

MySQL query - select from one, count from another table

From Dev

Select from one table and count from another

From Dev

mySQL SELECT from one table, JOIN from another, then SELECT from the new one

From Dev

MYSQL select from table and count from another

From Dev

MySQL - Join & Count rows from another table

From Dev

Join in MYSQL (Count from One table and list from one table)

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

select from one table, count from another where id is not linked

From Dev

MySQL select * from one table based on stock from another table

From Dev

MySQL count of count, use result from one table with another

From Dev

MySQL query with COUNT and join column from another table

From Dev

MYSQL query SELECT from one table and join results with other table

From Dev

PHP MySQL Select ID from one table and photo from another

From Dev

PostgreSQL select all from one table and join count from table relation

From Dev

Join a columns count from another table

From Dev

MySQL SELECT from one table and INSERT in another - Performance

From Dev

how to select a record from one table and count the number of which appears another table and count it

From Dev

Select data (join?) from only one table using an id from another table

From Dev

How to select / join some data from one mySQL innodb table to another with no duplicates and choosing the last inserted row per id

From Dev

MySql get count(*) with a join from a table

From Dev

SQLite select and count from another table with like

From Dev

Select * as well as count/sum from another table

From Dev

SQL Join (all from one table and one from another)

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

MySQL select data from table and inner join the union all selection from another two identical tables

From Dev

MySQL-Need to select a row from one table to another table WHERE a <# will generate in SELECT statement

From Dev

Display the details from one table and count from another table

From Dev

mysql - count from different table supplemented with another count from subresult

Related Related

  1. 1

    MYSQL join one colum from one table to two count() in another

  2. 2

    MySQL query - select from one, count from another table

  3. 3

    Select from one table and count from another

  4. 4

    mySQL SELECT from one table, JOIN from another, then SELECT from the new one

  5. 5

    MYSQL select from table and count from another

  6. 6

    MySQL - Join & Count rows from another table

  7. 7

    Join in MYSQL (Count from One table and list from one table)

  8. 8

    MySQL join / sum value from one table colum with a count values from another

  9. 9

    select from one table, count from another where id is not linked

  10. 10

    MySQL select * from one table based on stock from another table

  11. 11

    MySQL count of count, use result from one table with another

  12. 12

    MySQL query with COUNT and join column from another table

  13. 13

    MYSQL query SELECT from one table and join results with other table

  14. 14

    PHP MySQL Select ID from one table and photo from another

  15. 15

    PostgreSQL select all from one table and join count from table relation

  16. 16

    Join a columns count from another table

  17. 17

    MySQL SELECT from one table and INSERT in another - Performance

  18. 18

    how to select a record from one table and count the number of which appears another table and count it

  19. 19

    Select data (join?) from only one table using an id from another table

  20. 20

    How to select / join some data from one mySQL innodb table to another with no duplicates and choosing the last inserted row per id

  21. 21

    MySql get count(*) with a join from a table

  22. 22

    SQLite select and count from another table with like

  23. 23

    Select * as well as count/sum from another table

  24. 24

    SQL Join (all from one table and one from another)

  25. 25

    MySQL project design - conditionally select from one table based on rows from another select query

  26. 26

    MySQL select data from table and inner join the union all selection from another two identical tables

  27. 27

    MySQL-Need to select a row from one table to another table WHERE a <# will generate in SELECT statement

  28. 28

    Display the details from one table and count from another table

  29. 29

    mysql - count from different table supplemented with another count from subresult

HotTag

Archive