简体   繁体   中英

Mysql query LEFT JOIN unexpected result

mysql SELECT query with left join is not producing the result I am expecting. I hope someone can show me or point me to the right direction,

I am trying to build a query where I get all the users name from the "users" table and fetch the sum of all the time they spent for a particular date from the master table. I've used the left join but I am not getting the result as expected.

 SUM(m.time_spent) as sum_total_time
 FROM master as m
  LEFT OUTER JOIN users as u ON u.user_id = m.user_id
   WHERE m.date_created >= '2016-05-09'
  AND m.date_created <= '2016-05-13'
 GROUP BY name
 ORDER BY name

master table

master_id       user_id         time_spent               date_created
1                     1              40                   2016-05-01
2                     2             36                   2016-05-02
3                     3              56                   2016-05-03
4                     2             33                   2016-05-03
5                     1              32                   2016-05-05
nth                 nth       nth number                    nth date

users table

user_id            first_name            last_name
1                     James                 Green
2                     Robert                 Cox 
3                    Andy                    Roger
etc                   etc                    etc

I want the output result should look like this:

user_id       Name               sum_total_time

1               James Green          62
2               Robert Cox           69
3               Andy Roger           56
4               Brian Harper         0
5               Angel Lee            0
6               Andrew Martin        55
..... 
.....
Nth Name                             Nth value

You have to select data directly from the master table, group by user and calculate the sum. Then you can join this result with the user table to get all the information about the user.

Could be date conversione issue ..

 SUM(m.time_spent) as sum_total_time
 FROM master as m
  LEFT OUTER JOIN users as u ON u.user_id = m.user_id
   WHERE m.date_created >=STR_TO_DATE( '2016-05-09', '%Y-%m-%d)

and/or you have also incomplete sql

SUM(m.time_spent) as sum_total_time
 FROM master as m
  LEFT OUTER JOIN users as u ON u.user_id = m.user_id
   WHERE m.date_created >= '2016-05-09'
  AND m.date_created  // this condition don't match with nothing.. 
                      // could be you forgot a part 

Update 1
If you want user totale then

   select u.id,   u.name, SUM(m.time_spent) as sum_total_time
   FROM master as m
   Inner  JOIN users as u ON u.user_id = m.user_id
   WHERE m.date_created >=STR_TO_DATE( '2016-05-09', '%Y-%m-%d)
   AND m.date_created <= =STR_TO_DATE('2016-05-13'', '%Y-%m-%d)
   Group by u.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM