如何使用 JOIN 检索所有条目?

发布于 2024-12-09 16:33:33 字数 1163 浏览 1 评论 0原文

我的问题是:

enter code here

我有一个名为 test 的表,如下所示

id|  service  |   sub service  | Qt  |   date

1 | service_1 | sub_service_11 |  3  | 2011-12-03
2 | service_1 | sub_service_12 |  6  | 2011-12-03
3 | service_1 | sub_service_13 |  4  | 2011-12-03

后来我有一个名为 datedim 的表,如下所示

id|   date 

1 | 2011-12-01
2 | 2011-12-02
3 | 2011-12-03
4 | 2011-12-04
5 | 2011-12-05

我想要做的是,对于每个 sub_service,即使没有匹配项,也会从 datedim 返回所有日期。

所以基本上看起来像这样的东西

sub_service_11 | 2011-12-01 | NULL
sub_service_11 | 2011-12-02 | NULL
sub_service_11 | 2011-12-03 | 3
sub_service_11 | 2011-12-04 | NULL
sub_service_11 | 2011-12-05 | NULL
sub_service_12 | 2011-12-01 | NULL
sub_service_12 | 2011-12-02 | NULL
sub_service_12 | 2011-12-03 | 6
sub_service_12 | 2011-12-04 | NULL
sub_service_12 | 2011-12-05 | NULL
sub_service_13 | 2011-12-01 | NULL
sub_service_13 | 2011-12-02 | NULL
sub_service_13 | 2011-12-03 | 4
sub_service_13 | 2011-12-04 | NULL
sub_service_13 | 2011-12-05 | NULL

我确实尝试了 RIGHT JOIN、UNIONS 之类的东西,但我无法弄清楚。

有谁知道我怎样才能做到这一点?

谢谢你,

There is my problem :

enter code here

I have a table called test that looks like this

id|  service  |   sub service  | Qt  |   date

1 | service_1 | sub_service_11 |  3  | 2011-12-03
2 | service_1 | sub_service_12 |  6  | 2011-12-03
3 | service_1 | sub_service_13 |  4  | 2011-12-03

Later I have a table called datedim that looks like this

id|   date 

1 | 2011-12-01
2 | 2011-12-02
3 | 2011-12-03
4 | 2011-12-04
5 | 2011-12-05

What I am trying to do is that for each sub_service bring back all the date from datedim even if there is no match.

So basically something that would look like this

sub_service_11 | 2011-12-01 | NULL
sub_service_11 | 2011-12-02 | NULL
sub_service_11 | 2011-12-03 | 3
sub_service_11 | 2011-12-04 | NULL
sub_service_11 | 2011-12-05 | NULL
sub_service_12 | 2011-12-01 | NULL
sub_service_12 | 2011-12-02 | NULL
sub_service_12 | 2011-12-03 | 6
sub_service_12 | 2011-12-04 | NULL
sub_service_12 | 2011-12-05 | NULL
sub_service_13 | 2011-12-01 | NULL
sub_service_13 | 2011-12-02 | NULL
sub_service_13 | 2011-12-03 | 4
sub_service_13 | 2011-12-04 | NULL
sub_service_13 | 2011-12-05 | NULL

I did try RIGHT JOIN, UNIONS and stuff but I can't figure it out.

Does anyone know how I can accomplish that ?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只等公子 2024-12-16 16:33:33

这就是您所需要的:

select 
  ss.sub_service, 
  dd.date,
  ts.qt
from 
  (datedim dd, (select distinct sub_service from test) ss)
 left join test ts on (dd.date = ts.date and ts.sub_service = ss.sub_service)
order by ss.sub_service, dd.date

This is what you need:

select 
  ss.sub_service, 
  dd.date,
  ts.qt
from 
  (datedim dd, (select distinct sub_service from test) ss)
 left join test ts on (dd.date = ts.date and ts.sub_service = ss.sub_service)
order by ss.sub_service, dd.date
沧笙踏歌 2024-12-16 16:33:33

由于您有一个包含每个 sub_service 的表(我们称之为 sub_service_list),您应该能够执行以下操作:

SELECT
s.sub_service, d.date, t.Qt
FROM (sub_service_list s,datedim d) 
LEFT JOIN test t ON s.sub_service=t.sub_service AND d.date=t.date

As you have a table containing each sub_service (lets call it sub_service_list) you should be able to do the following:

SELECT
s.sub_service, d.date, t.Qt
FROM (sub_service_list s,datedim d) 
LEFT JOIN test t ON s.sub_service=t.sub_service AND d.date=t.date
短暂陪伴 2024-12-16 16:33:33

你可以这样做:

select t.id, sub_service, case when d.date = t.date then qt else null end as qt
from test t cross join datedim d 
order by sub_service, d.date

you can do this:

select t.id, sub_service, case when d.date = t.date then qt else null end as qt
from test t cross join datedim d 
order by sub_service, d.date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文