douwen5246 2019-05-21 22:42
浏览 138

如果没有要在第二个表中计数的匹配条件的行,如何显示一个表中的所有行

I have two tables. "members" is list of all members, "stats" is a list of dates worked. The shared field is memberID. I need a COUNT of the number of days each person worked and I want everyone listed in the output table, even if they have not yet had a work day.

Simplified database structure is:

**members**                 **stats**       
memberID lname    fname     memberID    date    statsID
1        Mertz    Fred      1        2017-12-31    1
2        Doe      Jane      3        2017-12-31    2
3        Smith    Frank     4        2017-12-31    3
4        Ricardo  Lucy      2        2018-12-31    4
5        Starr    Ringo     4        2018-12-31    5
                            2        2019-05-05    6
                            3        2019-05-05    7

Output desired is:

memberID  lname  fname  Total Days 
2         Doe    Jane       2 
1         Mertz  Fred       1 
4         Ricardo Lucy      2 
3         Smith   Frank     2 
5         Starr   Ringo     0 OR blank

Ringo has not yet worked any days and does NOT appear on the output table.

My code is:

$sql = "SELECT  u.*,
   COUNT(s.memberID)as tot_days
   FROM members u
   LEFT JOIN stats s
    ON s.memberID = u.memberID 
    GROUP BY s.memberID
    ORDER BY lname,fname";

$members = mysqli_query($dbc,$sql) or die(mysqli_error());

while ($row = mysqli_fetch_array($members)){ 
        $row = array_map('htmlspecialchars', $row);
        echo <<< HTML  etc.

This does everything I want it to do EXCEPT include those members who have not yet worked a day. JOIN, LEFT JOIN, LEFT OUTER JOIN, RIGHT JOIN, RIGHT OUTER JOIN all produce the same result. I tried LEFT and RIGHT INNER JOIN, if those even exist, which produced error Warning: mysqli_error() expects exactly 1 parameter, 0 given.

Someone suggested using COALESCE (COUNT(s.memberID),0) as tot_daysbut that just produces the same error as above.

I've been at this for days and am getting just a teensy bit frustrated!

  • 写回答

2条回答 默认 最新

  • doubi2145 2019-05-22 05:48
    关注

    u will have to reframe your query

    create table members (id int primary key auto_increment, lname varchar(30), fname varchar(30));
    insert into members values (1, 'Mertz',   'Fred') ,(2, 'Doe',     'Jane') ,(3, 'Smith',   'Frank'),(4, 'Ricardo', 'Lucy') ,(5, 'Starr',   'Ringo');
    
    
    create table stats (member_id int, dates date, stat_id int);
    insert into stats values (1, '2017-12-31', 1),(3, '2017-12-31', 2),(4, '2017-12-31', 3),(2, '2018-12-31', 4),(4, '2018-12-31', 5),(2, '2019-05-05', 6),(3, '2019-05-05', 7);
    
    
    
    mysql> (select 
        -> m.id,
        -> m.fname,
        -> count(*) as total_days
        -> from 
        -> members m inner join stats s on m.id = s.member_id
        -> group by m.id)
        -> union
        -> (select
        -> m.id,
        -> m.fname,
        -> 0 as total_days
        -> from
        -> members m where not exists ( select * from stats s where m.id = s.member_id));
    +----+-------+------------+
    | id | fname | total_days |
    +----+-------+------------+
    |  1 | Fred  |          1 |
    |  3 | Frank |          2 |
    |  4 | Lucy  |          2 |
    |  2 | Jane  |          2 |
    |  5 | Ringo |          0 |
    +----+-------+------------+
    5 rows in set (0.00 sec)
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用