与主键和外键有关的ADO查询表的问题 [英] Problem with ADO quering tables related with primary+foreign key
问题描述
大家好!
我参与了一个涉及动态SQL使用的项目,结果出现了问题.
简单的查询,联接,更改和其他工作都很好,但这是问题所在:
假设我有2个表,这些表通过主+外键关联.
让我将它们分别设为表A和表B.
键入SQL查询后,从A,B中选择data_from_A,data_from_B;"我从两个表中得到数据的笛卡尔积.
要进一步阐明:
假设表A保留姓名,表B保留薪水.
一旦开始上述查询,我应该得到以下内容:
亚历克斯| 1000
最高| 2000
.
.
.
但我得到了:
亚历克斯| 1000
亚历克斯| 2000
最高| 1000
最高| 2000
.
.
.
等等.
任何参考/代码示例都将受到高度赞赏.
我使用C ++,并且使用过ADO.
与教授讨论后,我们确定他做错了,因为我打字时没有认真注意.
因此,nv3建议的解决方案是唯一可能的方法.
因此,请按照回答关闭此线程.
再次感谢nv3!
Hello,everyone!
I have worked on a project which involves dynamic SQL usage, and there came out an issue.
Simple queries,joins,alter and stuff work well,but this is the issue:
Let us say I have 2 tables,related via primary+foreign key.
Let me dim them as table A and table B.
Once I type SQL query "select data_from_A,data_from_B from A,B;" I get cartesian product of data from both tables.
To clarify even more:
Lets say table A holds names,and table B their salaries.
Once the above query is started I should get the following:
Alex | 1000
Max | 2000
.
.
.
yet I get:
Alex | 1000
Alex | 2000
Max | 1000
Max | 2000
.
.
.
and so on.
Any reference/code example is highly appreciated.
I worked in C++, and have used ADO.
After discussion with professor,we have determined that he made mistake,since he didn''t pay attention carefully when I was typing.
Thus,the solution suggested by nv3 is the only possible way.
Therefore I close this thread as answered.
THANKS AGAIN nv3!
推荐答案
您应该在SQL语句中添加一个where子句,例如
You should put a where-clause in your SQL statement, like
... WHERE B.name = A.name ...
从笛卡尔乘积中仅选择与逻辑上链接表的键相关的那些元组.
to select only those tuples from the cartesian product that match regarding the key that links the tables logically.
这篇关于与主键和外键有关的ADO查询表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!