查询从两个表中获取记录的查询 [英] query which take records from two tables

查看:65
本文介绍了查询从两个表中获取记录的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想编写一个查询,该查询将一些来自table1的数据和一些来自table2的数据显示在dataGridview.i中,我读取了(INNER JOIN)语句,但是我想知道它比较了两个表中的记录,然后显示它们,但是我只想从两个表中获取一些记录并显示它们(不进行比较).
请帮助我
非常感谢


i want to write a query which take some datas from table1 and some from table2 and shows them in dataGridview.i read the (INNER JOIN) statement but i thinck that it compare the records from two tables and then shows them ,but i want just take some records from two tables and show them (not to compare them).
plz help me
thanks a lot

推荐答案

有两种方法可以做同一件事!
1>>使用经典和编码解决方案.这将有助于更深入地了解ddata边界!
2>>通过从两个表>>中生成视图来使用Ado.Net简易解决方案.对Visual Studio的要求更高!

//我应该选择更多概念性的方法,而不是简单快捷地>>
//但完全由您决定,我已经发布了这两个消息!



1>>经典方法:

除非您不对数据模式进行更深入的规范化,否则您将无法进行同样的操作


对于此,请遵循几个步骤>>

1.尝试规范化您的数据架构/
//例如:如果您有两个不同的表,则在两个表中都分配一个唯一的ID作为主键,然后仅使用那些相同的主键创建一个新表. IE;将您的架构分解为更正常的形式.

2.为您尝试露营,先将桌子引向联合表,再从联合表引向最后.这将帮助您从两个表中获取数据.



解释>>
SQL联接

根据这些表中某些列之间的关系,在SQL语句中使用JOIN关键字从两个或多个表中查询数据.

数据库中的表通常通过键相互关联.

主键是一列(或列的组合),每行具有唯一的值.每个主键值在表中必须唯一.目的是跨表将数据绑定在一起,而无需在每个表中重复所有数据.

查看人员"表:
P_Id姓氏名字地址城市
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3佩特森·卡里·斯托格20斯塔万格

请注意,"P_Id"列是人"表中的主键.这意味着没有两行可以具有相同的P_Id. P_Id可以区分两个人,即使他们的名字相同.

接下来,我们有订单"表:
O_Id订单编号P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

请注意,"O_Id"列是订单"表中的主键,并且"P_Id"列是指人员"表中的人员,而不使用其名称.

请注意,上面两个表之间的关系是"P_Id"列.



这可能对您有很大帮助!


2>>简单的解决方案

//***重要的是,如果您使用的是Ado.net,请尝试Genre-ting视图

>>在veiw表中包括来自两个表的数据,然后在数据网格视图中将veiw绑定为您的源,这将对您有所帮助....

如有其他问题,请联系anurag.smart4u@gmail.com
There are two ways for doing the same thing !
1>> using classical and coding solution >> It will help up understanding ddata bounding more deeply !
2>> using Ado.Net easy solution by Genrating views from both the table >> More depndent on visual studio !

// I would surly go for More conceptual methad rather than fast and easy >>
// but its Totaly upto you I had posted both !!!




1>> clasical Method :

Unless you dont tranform your data schema in more deep normalization for you will not be able to do the same


For THis follow few steps >>

1. try normalizing you data schema /
//for example : if you have two different table assign a unique id in both the table as primary key and then create a new table with only those same primary keys . i.e; breaking your schema into more normal form .

2. try campareing for you firt table to joint table then from join table to last . this will help you in getting data from bth the tables .



Explanantion >>
SQL JOIN

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.

Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Look at the "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.

Next, we have the "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15

Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column refers to the persons in the "Persons" table without using their names.

Notice that the relationship between the two tables above is the "P_Id" column.



this could help you a lot !


2>>easy solution in seconds

//*** Importantly if you are using Ado.net try Genre-ting views

>> in veiw table include the data from both the tables and then In data grid view bound the veiw as your source this will defently help you ....

For any more problems Contact anurag.smart4u@gmail.com


您好,

尝试使用 DISTINCT WHERE 子句
如果可以的话...
例如:
Hi,

Try to use DISTINCT and WHERE clause
if it could help...
As example:
StringBuilder sbQry = new StringBuilder();
sbQry.Append("select DISTINCT A.CorpCode, B.PlanCode, A.EmpName ");
sbQry.Append("FROM DBO.Employee A, DBO.EmpPlan B ");
sbQry.Append("WHERE  A.EmployeeId=B.EmployeeId ");

string qry = sbQry.ToString();



请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.

问候,

代数



Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem


这篇关于查询从两个表中获取记录的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆