简体   繁体   English

从SQL Server 2008中的表中检索数据

[英]Retrieving data from the table in SQL Server 2008

I have 4 tables 我有4张桌子

  1. table1 ( id, stateId(fk), name, carId(fk) ) table1id, stateId(fk), name, carId(fk)
  2. table2 ( stateId(pk), state, countryId(fk) ) table2stateId(pk), state, countryId(fk)
  3. table3 ( countryId, country, currency ) table3countryId, country, currency
  4. table4 ( carId, car ) table4carId, car

I want to retrieve name, state, country, car from the above tables through stored procedure using joins. 我想通过存储过程使用联接从上表中检索名称,州,国家,汽车。 And if some other easy way then please tell. 如果还有其他简单的方法,请告诉。

thanks. 谢谢。

A simple JOIN should do: 一个简单的JOIN应该做:

CREATE PROCEDURE schema.YourStoredProcedureName
AS
SELECT t1.name, t2.state, t3.country, t4.car
FROM table1 t1
    JOIN table2 t2 ON t1.stateId = t2.stateId
    JOIN table3 t3 ON t2.countryId = t3.countryId
    JOIN table4 t4 ON t1.carId = t4.carId
GO

No other easy method. 没有其他简单的方法。 Join is basic and easy ( http://msdn.microsoft.com/en-us/library/ms191517(v=sql.105).aspx ) 加入是基本且简单的( http://msdn.microsoft.com/zh-cn/library/ms191517(v=sql.105).aspx

select tbl1.name, tbl2.state, tbl3.country, tbl4.car
From table1 tbl1 
inner join table2 tbl2 on tbl1.stateid =tbl2.stateid   
inner join table3 tbl3 on tbl2.countryid = tbl3.countryid
inner join table4 tbl4 on tbl1.carId = tbl4.carId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM