SQL 连接一对多关系
好吧,这是我原来的问题;
表一包含
ID|Name
1 Mary
2 John
表二包含
ID|Color
1 Red
1 Blue
2 Blue
2 Green
2 Black
我想最终得到 is
ID|Name|Red|Blue|Green|Black
1 Mary Y Y
2 John Y Y Y
似乎因为表一中有 11 个唯一的颜色值和 1000 条记录,所以没有“好的”方法可以做到这一点。那么,还有两个问题。
有没有一种有效的方法来查询以获得这个结果?然后,我可以在应用程序中创建一个交叉表以获得所需的结果。
ID|Name|Color
1 Mary Red
1 Mary Blue
2 John Blue
2 John Green
2 John Black
如果我想限制返回的记录数,我该如何执行查询来执行类似的操作?
Where ((color='blue') AND (color<>'red' OR color<>'green'))
因此,使用上面的示例,我将
ID|Name|Color
1 Mary Blue
2 John Blue
2 John Black
通过 ADODB 连接到 Visual FoxPro 表以使用 SQL。谢谢!
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. 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. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您之前的问题,并查询 VFP 表,您可以通过以下方式获得结果
以下 VFP 限定查询...交叉表完成
然后,由于您有其他“颜色”,只需复制相应颜色的 MAX( IIF()) 并将该列作为结果列名称...遵循模式。唯一的问题是,如果您对颜色有不同的区分大小写的拼写,那么您可能需要 UPPER( C.Color ) = "RED" (或类似的其他颜色)
From your prior question, and querying against a VFP table, you could get your results by
the following VFP qualified query... cross-tab complete
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. 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)
您正在寻找进行交叉表查询。您可以尝试使用交叉表查询向导:
http://msdn.microsoft.com/en-我们/库/aa979431%28VS.71%29.aspx
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