简体   繁体   English

SQL SELECT语句,用于从具有外键和关联关系的多个表中提取数据

[英]SQL SELECT statement to pull data from multiple tables with foreign keys and associative relationships

I have the following tables: 我有以下表格:

  1. Universities( iduni, uniname )
  2. Campuses( idcampus, iduni, campusname, campusaddress )
  3. Projects( idproject, idcampus, projectname )
  4. IndustryCode( idindustrycode, codenumber, description )
  5. Specialization( idspec, specname )

The following are the associative tables for some of the above listed tables 以下是一些上面列出的表的关联表

  1. CampusSpecialization( idcampus, idspec )
  2. ProjectSpecialization( idproject, idSpec )
  3. ProjectIndustryCode( idproject,idindustrycode )

And I want to generate XML file with the following structure via PHP, but can't figure out the SELECT statement to pull the data from the database. 我想通过PHP生成具有以下结构的XML文件,但无法找出从数据库中提取数据的SELECT语句。 The data being the campuses, universities, projects, industry codes and specializations for each project so that when I can filter the results in my small application later on based on this interconnected data. 数据是每个项目的校园,大学,项目,行业代码和专业化,以便我可以在以后基于此互连数据在我的小应用程序中过滤结果。

    <items>
        <item>
            <campus campusname="$CAMPUSNAME" uniname="$UNINAME" campusaddress="$CAMPUSADDRESS">
                <projects>
                    <project>
                        <name>$PROJECTNAME</name>
                        <specs>
                            <spec>$SPECNAME1</spec>
                            <spec>$SPECNAME2</spec>
                            ...
                        </specs>
                        <industries>
                            <industry>$CODENUMBER1</industry>
                            <industry>$CODENUMBER2</industry>
                            ...
                        </industries>
                    </project>
...
                </projects>
            </campus>
        </item>
        <item>
            <campus ... >
                <projects>
                    <project ... >
                        ...
                    </project>
            </campus>
    </items>

This is my SQL statment so far: 到目前为止,这是我的SQL语句:

SELECT
  campusname, 
  specname,
  projectname
FROM
  Specialization,Projects
  JOIN CampusSpecialization ON CampusSpecialization.idspec = Specialization.idspec
  JOIN Campuses ON CampusSpecialization.idcampus = Campuses.idcampus
  JOIN ProjectSpecialization ON ProjectSpecialization.idspec = Specialization.idspec

EDIT: 编辑:

Projects may have one or more codes and industries. 项目可能有一个或多个代码和行业。

try this: 尝试这个:

SELECT c.campusname,u.uniname,c.campusaddress, s.specname, ic.codenumber, p.projectname
FROM campuses c
INNER JOIN universities u ON u.iduni = c.iduni
INNER JOIN camousspecialization cs ON cs.idcampus = c.idcampus
INNER JOIN speicialization s ON s.idspec = cs.idspec
INNER JOIN projectspecialization ps ON ps.idproject = s.idspec
INNER JOIN projectindustrycode pi On pi.idproject = ps.idproject
INNER JOIN industrycode ic on ic.industrycode = pi.industrycode
INNER JOIN projects p on p.idproject = ps.idproject 

Not tested, but this should pull all campuses with projects, and the project's codeNumber and specName. 没有经过测试,但这应该将所有校园与项目, 项目的 codeNumber和specName拉开。

Note, I am not completely sure about your schema. 注意,我不完全确定您的架构。 The associative tables imply there may be multiple codes and specNames per project. 关联表意味着每个项目可能有多个代码和specNames。 Whereas the xml implies there is only one of those per project. 而xml意味着每个项目只有一个。 This query will return one record for each entry in your associative tables. 此查询将为关联表中的每个条目返回一条记录。 ie If there are three (3) specifications for "Project A", the SELECT will return three (3) records. 即如果“项目A”有三(3)个规范,SELECT将返回三(3)条记录。

SELECT  c.idCampus
        , c.campusName
        , c.campusAddress
        , u.uniName
        , p.projectName
        , s.specName as ProjectSpecName
        , ic.codeNumber AS ProjectCodeNumber
FROM    Campuses c
            INNER JOIN Universities u ON u.iduni = c.iduni
            INNER JOIN Projects p ON p.idcampus = c.idcampus
            INNER JOIN ProjectSpecialization ps ON ps.idproject = p.idproject
            INNER JOIN Specialization s ON s.idspec = ps.idspec
            INNER JOIN ProjectIndustryCode pic ON pic.idproject = p.idproject
            INNER JOIN IndustryCode ic ON ic.idindustrycode = pic.idindustrycode

Edit: Unfortunately, I do not use php. 编辑:不幸的是,我不使用PHP。 So I do not know the best or standard way to generate xml in php. 所以我不知道在php中生成xml的最佳或标准方法。 You could possibly use group_concat in your SELECT to return a concatenated list of the codes and names for each project. 您可以在SELECT中使用group_concat来返回每个项目的代码和名称的连接列表。 Then split those string into an array in php and use the array count to generate the desired number of xml elements. 然后将这些字符串拆分为php中的数组,并使用数组计数生成所需数量的xml元素。

SELECT  c.idCampus
        , c.campusName
        , c.campusAddress
        , u.uniName
        , p.projectName
        , GROUP_CONCAT(s.specName SEPARATOR '|') as SpecNameList
        , GROUP_CONCAT(ic.codeNumber SEPARATOR '|') AS CodeNumberList
       ...

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

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