在不使用连接的情况下从多个表中检索数据 [英] Retrive data from more than one table without using joins

查看:52
本文介绍了在不使用连接的情况下从多个表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i我有两张桌子..

1)员工表列是EmpId,EmpName,CityId ....

2)城市表列是CityId,CityName ...



现在我想从这两个中获取employeeId,员工姓名,城市名称没有使用连接的表...



是否可能...

解决方案

这是可能的。试试这个



选择a.EmpId,a.EmpName,a.CityId,b.CityId,b.CityName from employeableable as a,citytable as b 


嗯......是的。但这是一个愚蠢的想法!



如果你有两个表之间的链接,那么你可能想要返回一行只与该行相关的数据 - 并且最好的方法是使用 JOIN

如果你不返回相关的行:

  SELECT  *  FROM  MyTable,MyTable2 

然后它只能为MyTable的每一行重新输入MyTable2的每一行:所以如果MyTable中有6行,MyTable2中有2行,它将返回12行。



如果您使用联接:

  SELECT  *  FROM  MyTable m1 
JOIN MyTable2 m2 ON m1.ID = m2.Id

然后它只返回你感兴趣的行。

如果你真的很绝望,你可以在没有加入的情况下这样做:

 < span class =code-keyword> SELECT  *  FROM  MyTable,MyTable2  WHERE  MyTable。 ID = MyTable2.Id 

它将返回相同的行。



在性能方面,没有区别,但是 JOIN 表格更容易维护,特别是如果查询变得更复杂。

良好做法建议 JOIN 超过 WHERE 表格。


访问这里这将有助于你



http :/ // hlog =http:/ /blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/target =_ blanktitle =New Window> ^

Hi,

i am having two tables..
1) employee table columns are EmpId,EmpName,CityId....
2) City table columns are CityId,CityName...

Now i want to fetch employeeId,Employee name,cityname from these two tables without using joins...

is it possible ...

解决方案

It is possible.try this

select a.EmpId,a.EmpName,a.CityId,b.CityId,b.CityName from employeetable as a,citytable as b


Well...yes. But it's a stupid idea!

If you have a link between the two tables, then you presumably want to return data on a row that is only relevant to that row - and the best way to do that is with a JOIN.
If you don't return only the relevant rows:

SELECT * FROM MyTable, MyTable2

Then it can only retirn each row of MyTable2 for each row of MyTable: so if you have 6 Rows in MyTable and 2 in MyTable2, the it will return 12 rows.

If you use a join:

SELECT * FROM MyTable m1
JOIN MyTable2 m2 ON m1.ID=m2.Id

Then it returns only the rows you are interested in.
You could do it without a JOIN if you were really desperate:

SELECT * FROM MyTable, MyTable2 WHERE MyTable.ID=MyTable2.Id

And it will return the same rows.

In performance terms, there is no difference, but the JOIN form is a lot easier to maintain, particularly if the query becomes more complex.
Good practice recommends the JOIN over the WHERE form.


visit here this will help you

http://blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/[^]


这篇关于在不使用连接的情况下从多个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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