简体   繁体   English

vb.net中的Ms.Access查询从tbl1中选择全部,从tbl2中仅选择一列

[英]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

Create two tables as following: 创建两个表,如下所示:

在此处输入图片说明

and then use this query 然后使用此查询

select t1.ID, t1.Name, t1.Sex, t2.OrderDate, t2.OrderCode from 
Table1 t1
inner join Table2 t2 on t1.ID = t2.ID;

请注意,您的表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.

相关问题 从tbl1中未列出的tbl1中选择 - Select From tbl1 not listed in tbl2 mysql插入或选择到-将列从tbl1复制到tbl2 - mysql insert into or select into - copy column from tbl1 into tbl2 SQL Server - 从 tbl1 插入 tbl2,其中 tbl1.column1 &lt;&gt; tbl2.column1 - SQL Server - Insert into tbl2 from tbl1 where tbl1.column1 <> tbl2.column1 仅当字段在tbl2中不匹配时才在tbl1中选择 - Select in tbl1 only if fields don't match in tbl2 从 tbl1 获取所有记录并从 tbl2 获取匹配(或下一个关闭)记录,匹配基于 Client# 和从 tbl1 到 tbl2 的日期 - Get all records from tbl1 and matching (or the next close) records from tbl2, match is based on Client# and Date from tbl1 to tbl2 SQL - 连接2个表,根据条件显示来自tbl1的所有数据和来自tbl2的数据 - SQL - Joining 2 tables, show all from tbl1 and data from tbl2 depending on condition 在不使用“NOT”关键字的情况下,从 #tbl1 中获取所有不在 #tbl2 中的记录 - Fetch all records from #tbl1 which are not in #tbl2 without use of 'NOT' keyword 当tbl1的列为true时将tbl1的PK用作tbl2的PK - Use as PK on tbl2 a PK of tbl1 with the condition when a column of tbl1 is true 将tbl1与select语句连接两次,并同时连接到tbl2和tbl3 - Joining tbl1 to select statement twice with join to tbl2 that also joins to tbl3 当TBL2中的所有引用都等于给定值时,更新TBL1中的1条记录 - Update 1 record in TBL1 when all references in TBL2 are equal to a given value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM