开发者

Using SQL JOINs

I've never used joins before, but I've been reading for the past couple of hours but can't seem to get things to work how I'd like them to.

I have 2 tables, in different databases, I need to return data from both of them in one query.

I have a table called Player in the database kal_db, and one called Login in the database kal_auth

I need to return the 'Country开发者_开发知识库' field from Login where Player.UID = Login.UID, aswell as all the data from the Player table.

I've tried to do it many ways, and just can't make it work.

Any input?


select l.Country, p.* 
from kal_auth.dbo.Login l 
join kal_db.dbo.Player p 
on p.UID = l.UID


select l.Country,p.* from kal_db..Player p join kal_auth..Login l on l.UID=p.UID


Try this:

SELECT p.*, l.Country
FROM kal_db.dbo.Player as p
INNER JOIN kal_Auth.dbo.Login as l
ON p.UID = l.UID

I'd also recommend giving this article a good read:

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

It is the best explanation of the various types of joins I've seen and it goes into a bit of the syntax.


Try This.

          Select l.Country
          from kal_db.dbo.Player p 
          Inner Join 
          kal_Auth.dbo.Login l
          on p.UID = l.UID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜