[英]How to display all rows from one table if there are no rows with matching criteria to count in second table
I have two tables. 我有两张桌子。 "members" is list of all members, "stats" is a list of dates worked. “成员”是所有成员的列表,“统计数据”是工作日期的列表。 The shared field is memberID. 共享字段是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. 我需要每个人工作的天数COUNT,我希望每个人都在输出表中列出,即使他们还没有工作日。
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. Ringo任何时候都没有工作,也没有出现在输出表上。
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. JOIN,LEFT JOIN,LEFT OUTER JOIN,RIGHT JOIN,RIGHT OUTER JOIN都产生相同的结果。 I tried LEFT and RIGHT INNER JOIN, if those even exist, which produced error Warning: mysqli_error() expects exactly 1 parameter, 0 given
. 我试过LEFT和RIGHT INNER JOIN,如果那些甚至存在, Warning: mysqli_error() expects exactly 1 parameter, 0 given
产生了错误Warning: mysqli_error() expects exactly 1 parameter, 0 given
。
Someone suggested using COALESCE (COUNT(s.memberID),0) as tot_days
but that just produces the same error as above. 有人建议使用COALESCE (COUNT(s.memberID),0) as tot_days
但这只会产生与上面相同的错误。
I've been at this for days and am getting just a teensy bit frustrated! 我已经在这几天了,我只是一点点沮丧!
SELECT u.*
, COUNT(DISTINCT s.date) tot_days
FROM members u
LEFT
JOIN stats s
ON s.memberID = u.memberID
GROUP
BY u.memberID
ORDER
BY u.lname
, u.fname
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.