所以我有两张桌子。
select distinct id, doc
from images
看起来像:
ID | DOC
--------------------
GROWG APP
GROWG BACKGR
MAXAR APP
MAXAR BACKGR
MAXAR LIC
和
select DriverID
from drivers
看起来像
DriverID
--------
GROWG
ZULLY
MAXAR
JILLX
当我使用临时表或select语句时,我希望我的最终结果看起来像是这样:
DriverID | Doc | Count
-----------------------------------------
GROWG APP 1
GROWG BACKGR 1
GROWG LIC **NULL** (or 0)
MAXAR APP 1
MAXAR BACKGR 1
MAXAR LIC 1
所以最后它认识到总共有3种不同类型的文件,它表明GROWG错过了他的文件" LIC"
有人可以帮忙吗?
答案 0 :(得分:2)
我想你想要这样的东西:
select id.id, doc.doc, count(i.id) as cnt
from (select distinct id from images) as id cross join
(select distinct doc from images) doc left outer join
images i
on i.id = id.id and i.doc = doc.doc;
它会返回id
中doc
和images
的所有组合以及表格的计数。如果组合不存在,则返回0
。
编辑:
要检查与drivers
的匹配,您只需添加:
where id in (select driverid from drivers)
答案 1 :(得分:1)
我已经从Gordon Linoff修改了上述查询。检查一下
select id.driverid, doc.doc, count(i.id) as cnt
from (select distinct driverid from drivers) as id cross join
(select distinct doc from images) doc left outer join
images i
on i.id = id.driverid and i.doc = doc.doc
where id.driverid in (select id from images )
group by id.driverid,doc.doc;