从四个表中获取数据

时间:2012-08-08 11:53:01

标签: sql

如何从四个表中获取数据?

我有四张桌子:

  • Article_Table ArticleName, ArticleNumber
  • IndexDetails_Table IndexSubMasterId, ArticleNumber
  • IndexSubMaster_Table IndexSubMasterId, IndexMasterId
  • IndexMaster_Table IndexMasterId, Keyword

我需要最终输出:

ArticleName  from Article table. 

我将为您提供Keyword作为输入参数。

我需要的过程是:

  1. IndexMasterId From IndexMaster_Table where Keyword = 'bl'

  2. IndexSubMasterId From IndexSubMaster_Table
    Where IndexSubMaster_Table.IndexMasterId = IndexMaster_Table.IndexMasterId

  3. ArticleNumber From IndexDetails_Table Where IndexDetails_Table.IndexSubMasterId = IndexSubMaster_Table.IndexSubMasterId

  4. ArticleName From Article_Table
    Where Article_Table.ArticleNumber = IndexDetails_Table.ArticleNumber

1 个答案:

答案 0 :(得分:0)

试试这个我相信有更有效的方法,但这里有适合你的东西

Declare @keyword varchar(20)
Set @keyword = 'bl'

Select article.ArticleName FROM IndexMaster_Table masterTable
JOIN IndexSubMaster_Table subMaster
on masterTable.IndexMasterId = subMaster.IndexMasterId
JOIN IndexDetails_Table details
on details.IndexSubMasterId = subMaster.IndexSubMasterId
JOIN Article_Table article
on article.ArticleNumber = details.ArticleNumber
Where masterTable.keyword = @keyword