在不使用连接的情况下从多个表中检索数据 [英] Retrive data from more than one table without using joins
问题描述
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 aJOIN
.
If you don't return only the relevant rows:
SELECT * FROM MyTable, MyTable2Then 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.IdThen 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.IdAnd it will return the same rows.
In performance terms, there is no difference, but theJOIN
form is a lot easier to maintain, particularly if the query becomes more complex.
Good practice recommends theJOIN
over theWHERE
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屋!