简体   繁体   中英

Get the required sql query result

mysql> select calendar.calId,calendar.cdate,lsUsers.userId,
              apptActual.OPDID,count(*) 
        from calendar 
        LEFT JOIN apptActual on calendar.calId=apptActual.calendarId
        LEFT JOIN lsUsers on lsUsers.userId=apptActual.OPDID 
        group by 1,2,3,4 order by 3
        desc limit 10;

Result is:

+-------+------------+--------+-------+----------+
| calId | cdate      | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
|    76 | 2016-03-16 |     11 |    11 |        1 |
|    75 | 2016-03-15 |     11 |    11 |        1 |
|    77 | 2016-03-17 |      9 |     9 |        1 |
|    75 | 2016-03-15 |      8 |     8 |        1 |
|    75 | 2016-03-15 |      1 |     1 |        1 |
|  1279 | 2019-07-02 |   NULL |  NULL |        1 |
|  1287 | 2019-07-10 |   NULL |  NULL |        1 |
|  1295 | 2019-07-18 |   NULL |  NULL |        1 |
|  1303 | 2019-07-26 |   NULL |  NULL |        1 |
|  1311 | 2019-08-03 |   NULL |  NULL |        1 |
+-------+------------+--------+-------+----------+
10 rows in set (0.34 sec)

By using above query I am getting result as above, but I need result result like below (all userId's has to come.)

+-------+------------+--------+-------+----------+
| calId | cdate      | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
|    76 | 2016-03-16 |     11 |    11 |        1 |
|    75 | 2016-03-15 |     11 |    11 |        1 |
|    77 | 2016-03-17 |      9 |     9 |        1 |
|    75 | 2016-03-15 |      8 |     8 |        1 |
|    75 | 2016-03-15 |      1 |     1 |        1 |
|  1279 | 2019-07-02 |      4 |  NULL |        1 |
|  1287 | 2019-07-10 |      21|  NULL |        1 |
|  1295 | 2019-07-18 |      3 |  NULL |        1 |
|  1303 | 2019-07-26 |      7 |  NULL |        1 |
|  1311 | 2019-08-03 |      5 |  NULL |        1 |
+-------+------------+--------+-------+----------+

Please suggest me how to get result as above!

If you join lsUsers on userId = appActual.OPDID you will get what you ask for: usedId (NULL) = OPDID (NULL). Is there any other way to join lsUsers?

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