count where group by 查询问题求助
各位好,有个问题请教大家:
我想要的结果是没有注释where前的类似结果,即保证左连左表所有记录都在,没有记录的count以0显示,谢谢~
drop table ta;
drop table tb;
create table ta(
id int(4) primary key not null auto_increment,
user_id int (4),
name varchar(10)
);
create table tb(
id int(4) primary key not null auto_increment,
fid int(4) comment 'ref ta.id',
status int(1)
);
insert into ta(user_id,name) values(1,'lua');
insert into ta(user_id,name) values(1,'aix');
insert into ta(user_id,name) values(2,'java');
insert into ta(user_id,name) values(2,'cpp');
insert into ta(user_id,name) values(2,'csharp');
insert into ta(user_id,name) values(3,'linux');
insert into ta(user_id,name) values(9,'id');
insert into tb(fid,status) values(1,1);
insert into tb(fid,status) values(3,1);
insert into tb(fid,status) values(4,1);
insert into tb(fid,status) values(5,0);
insert into tb(fid,status) values(7,0);
select * from ta;
select * from tb;
select ta.id,ta.user_id,ta.name,tb.id as tbId,tb.fid,tb.status,count(tb.fid) countFid from ta left join tb on ta.id = tb.fid
-- where tb.status = 1
group by ta.id;
我想要的结果是没有注释where前的类似结果,即保证左连左表所有记录都在,没有记录的count以0显示,谢谢~
...全文
相关推荐
sql中的 where 、group by 和 having 用法解析
例如,如果你想知道每个部门的员工人数:```sqlSELECT Department, COUNT(*) AS EmployeeCountFROM EmployeesGROUP BY Department
mysql中count(), group by, order by使用详解
ORDER BY create_time DESC) AS temp_table GROUP BY from_company_id, from_user_id;```在这个查询中,外层的 `COUNT(
使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT
在数据库查询中,统计记录的数量是一个常见的需求,特别是在涉及到分组(GROUP BY)操作时。
浅谈sql语句中GROUP BY 和 HAVING的使用方法
o, customer c WHERE c.id = o.customer_id GROUP BY c.id HAVING COUNT(o.order_number) > 5; ```4.
Oracle中分组查询group by用法规则详解
例如,按部门和职位排序上述查询结果: ```sql SELECT deptno, job, COUNT(*) FROM emp GROUP BY deptno, job ORDER BY deptno,


