如何使用连接从4个表中获取数据

时间:2013-06-18 07:05:41

标签: mysql database

我使用以下查询从两个表中获取数据它工作正常,但我想从另外两个4个表中得到

$query = mysql_query("SELECT ml.UserID,ml.ContentAddedDateTime,cat.ContentType,cat.ContentTitle,cat.ContentSource,cat.ContentDescription
  FROM LibraryMaster as ml
  LEFT JOIN CatalogMaster cat ON cat.CatalogID=ml.CatalogID");

我想从SubCategoryLook中的CategoryLookUp和subCategoryTitle中取类别

这是CatalogMaster表

    CatalogID   int(11)         No      auto_increment                          
CategoryID  int(11)         No                                  
SubCategoryID   int(11)         No                                  
ContentType varchar(10) utf8_general_ci     No                                  
ContentTitle    varchar(25) utf8_general_ci     No                                  
AddedByUserID   int(11)         No                                  
ContentDescription  varchar(250)    utf8_general_ci     Yes NULL                                
ContentSource   varchar(250)    utf8_general_ci     No                                  
ContentAddedDateTime

这里是CatgeoryLookUp

    CategoryID  int(11)         No      auto_increment                          
CategoryTitle   varchar(100)    utf8_general_ci     No                                  
CategoryDescription varchar(250)    utf8_general_ci     Yes NULL                                
CategoryAddedByUserID   int(11)         No                                  
CategoryAddedDateTime   

这是SubCatLookUp

    SubCategoryID   int(11)         No      auto_increment                          
LinkedCategoryID    int(11)         No                                  
SubCategoryTitle    varchar(100)    utf8_general_ci     No                                  
SubCategoryAddedDateTime    timestamp       ON UPDATE CURRENT_TIMESTAMP No  CURRENT_TIMESTAMP                               
SubCategoryAddedByUserID

1 个答案:

答案 0 :(得分:0)

$query = mysql_query("
            SELECT ml.UserID, ml.ContentAddedDateTime, cat.ContentType, cat.ContentTitle, cat.ContentSource, cat.ContentDescription, clu.CategoryTitle, sclu.SubCategoryTitle
               FROM LibraryMaster as ml
                    LEFT JOIN CatalogMaster cat ON cat.CatalogID=ml.CatalogID
                    LEFT JOIN CategoryLookUp clu ON cat.CategoryID = clu.CategoryID
                    LEFT JOIN SubCatLookUp sclu ON cat.SubCategoryID = sclu.SubCategoryID
");

使用以上查询,