如何使用 JOIN 检索所有条目?
我的问题是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是您所需要的:
This is what you need:
由于您有一个包含每个 sub_service 的表(我们称之为 sub_service_list),您应该能够执行以下操作:
As you have a table containing each sub_service (lets call it sub_service_list) you should be able to do the following:
你可以这样做:
you can do this: