[英]Ms.Access query in vb.net select all from tbl1 and only 1 column from tbl2
I have 2 tables: 我有2张桌子:
tbl1(ID, Name, Sex, OrderDate)
tbl2(OrderDate, OrderCode)
I try to display all data from tbl1 (ID, Name, Sex, OrderDate)
and only one column from tbl2(OrderCode)
. 我尝试显示来自
tbl1 (ID, Name, Sex, OrderDate)
所有数据tbl1 (ID, Name, Sex, OrderDate)
以及来自tbl2(OrderCode)
仅一列。
I have tried this 我已经试过了
SELECT tbl1.*, tbl2.OrderCode FROM tbl1, tbl2;
but it shows duplicate data. 但它显示重复的数据。 like this
像这样
I have search a while but only see query of mySQL where they use join
but it appears syntax error clause. 我查了一段时间,但只看到MySQL的,他们使用的查询
join
,但它似乎语法错误条款。
I want it appears like this ID Name Sex OrderDate OrderCode OrderCode is autoNumber Random which is why I put it in other table since Access not allow 2 autoNumber in same table 我希望它看起来像这样ID名称性别OrderDate OrderCode OrderCode是autoNumber随机的,这就是为什么我将其放在其他表中的原因,因为Access不允许在同一表中使用2个autoNumber
You are doing a CROSS JOIN which results in a cartesian product 您正在执行CROSS JOIN,这会产生笛卡尔积
You should JOIN on the field that is common in the 2 tables to limit the rows returned. 您应该在2个表中通用的字段上进行JOIN操作,以限制返回的行。
In your case the only field I see on both tables is OrderDate, and that's a bit weird. 在您的情况下,我在两个表上看到的唯一字段是OrderDate,这有点奇怪。
Try this 尝试这个
SELECT tbl1.*, tbl2.OrderCode FROM tbl1, tbl2 WHERE tbl1.OrderDate=tbl2.OrderDate
请注意,您的表tbl1(ID,名称,性别,OrderDate)tbl2(OrderDate,OrderCode)没有关系,您必须先关联这两个表,然后才能选择列。.尝试以下操作:新建表:tbl1(ID,名称, Sex,OrderDate)tbl2(ID,OrderDate,OrderCode)tbl1.Id是主键,Tbl2.ID是外键,并且我将选择Tbl1中的所有列和tbl2中的一列(无论大小写限制)
Select tbl1.Id,tbl1.name,tbl1.sex,tbl1.Orderdate,tbl2.orderdate from tbl1,tbl2 where tbl1.id=tbl2.id
When you want to get a result that has a relationship with two tables, that means you want to use Join. 如果要获得与两个表有关系的结果,则意味着要使用Join。
you can also use distinct when you have redundant result. 如果您有多余的结果,也可以使用distinct。
SELECT DISTINCT tbl1.*, tbl2.OrderCode FROM tbl1, tbl2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.