[英]SQL Joining on a one-to-many relationship
Ok, here was my original question; 好吧,这是我最初的问题;
Table one contains 表一包含
ID|Name
1 Mary
2 John
Table two contains 表二包含
ID|Color
1 Red
1 Blue
2 Blue
2 Green
2 Black
I want to end up with is 我要结束的是
ID|Name|Red|Blue|Green|Black
1 Mary Y Y
2 John Y Y Y
It seems that because there are 11 unique values for color and 1000's upon 1000's of records in table one that there is no 'good' way to do this. 似乎因为表11中1000个记录中有11个颜色唯一值和1000个唯一值,所以没有“好的”方法来做到这一点。 So, two other questions.
因此,还有两个问题。
Is there an efficient way to query to get this result? 有没有一种有效的方法来查询以获得此结果? I can then create a crosstab in my application to get the desired result.
然后,我可以在应用程序中创建一个交叉表以获得所需的结果。
ID|Name|Color
1 Mary Red
1 Mary Blue
2 John Blue
2 John Green
2 John Black
If I wanted to limit the number of records returned how could I do a query to do something like this? 如果我想限制返回的记录数,该如何执行查询以执行类似的操作?
Where ((color='blue') AND (color<>'red' OR color<>'green'))
So using the above example I would then get back 因此,使用上面的示例,我将返回
ID|Name|Color
1 Mary Blue
2 John Blue
2 John Black
I connect to Visual FoxPro tables via ADODB to use SQL. 我通过ADODB连接到Visual FoxPro表以使用SQL。 Thanks!
谢谢!
You are looking to make a crosstab query. 您正在寻找一个交叉表查询。 You could try to use the crosstab query wizard:
您可以尝试使用交叉表查询向导:
http://msdn.microsoft.com/en-us/library/aa979431%28VS.71%29.aspx http://msdn.microsoft.com/zh-CN/library/aa979431%28VS.71%29.aspx
From your prior question, and querying against a VFP table, you could get your results by the following VFP qualified query... cross-tab complete 从上一个问题开始,并查询VFP表,可以通过以下VFP限定查询来获得结果...交叉表已完成
select
N.ID,
N.Name,
MAX( IIF( C.Color = "Red", "Y", " " )) Red,
MAX( IIF( C.Color = "Blue", "Y", " " )) Blue,
MAX( IIF( C.Color = "Green", "Y", " " )) Green,
MAX( IIF( C.Color = "Black", "Y", " " )) Black
FROM
C_Names N,
Colors C
WHERE
N.ID = C.ID
GROUP BY
N.ID,
N.Name
Then, as you have other "colors", just copy the MAX( IIF()) for that respective color and have the column as the result column name... follow the pattern. 然后,像其他“颜色”一样,只需复制该相应颜色的MAX(IIF())并将该列作为结果列名称...遵循该模式。 Only issue is if you have different case-sensitve spelling of the colors, then you may need to UPPER( C.Color ) = "RED" (or similar for other colors)
唯一的问题是,如果您对颜色的大小写区分不同,则可能需要UPPER(C.Color)=“ RED”(或其他颜色类似)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.