亲子表记录 - 构建SQL查询 [英] Parent Child table record - Building SQL query
本文介绍了亲子表记录 - 构建SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的表,这些表中的数据
Here is my table and data of these tables
表名称: code
CID Code
1 abc
2 def
3 xyz
表名称:详细信息
ID Name CID
1 a 1
2 b 2
产生的表:
ID Code Name
1 abc a
2 abc Null
3 def b
4 def Null
5 xyz Null
6 xyz Null
我NNED获得从code表,并针对每个code我必须从细节表中获取所有的行中的所有记录,如果有的code具有价值的需求值,如果没有的话空
I nned to get all record from the code table and against each code I have to get all the rows from the details table, if some code have value their need value and if not then Null
感谢
推荐答案
听起来你正在寻找的笛卡尔乘积:
Sounds like you're looking for the cartesian product:
SELECT
c.CID * d.ID AS ID,
c.Code,
CASE
WHEN c.CID = d.CID THEN d.Name
ELSE NULL
END AS Name
FROM Code c
CROSS JOIN Details d
虽然笛卡尔产品是较大的表相当缓慢......所以要确保这是你真正想要的。
Although cartesian products are quite slow for larger tables... so be sure that this is what you really want.
这篇关于亲子表记录 - 构建SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文