首页 > 解决方案 > 如何将这些查询与另一个父表中的 where 子句组合成单个查询?

问题描述

如何将这些查询与另一个父表中的 where 子句组合成一个查询?请考虑我的 SQL 代码并提出更好的使用方法

//look my code
    CREATE TABLE IF NOT EXISTS first (
       fid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
       p_name varchar(60) NOT NULL
    );
    CREATE TABLE IF NOT EXISTS second (
        sed int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
        firstname varchar(20) NOT NULL,
        fid int(11) NOT NULL,
        FOREIGN KEY (fid) REFERENCES first(fid)
    );
    CREATE TABLE IF NOT EXISTS third (
        thid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
        start_date date NOT NULL,
        end_date date NOT NULL,
        sed int(11) NOT NULL,
        FOREIGN KEY (sed) REFERENCES second(sed),
        fid int(11) NOT NULL,
        FOREIGN KEY (fid) REFERENCES first(fid)
    );
    CREATE TABLE IF NOT EXISTS fourth (
        fid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
        start_date date NOT NULL,
        end_date date NOT NULL,
        sed int(11) NOT NULL,
        FOREIGN KEY (sed) REFERENCES second(sed),
        fid int(11) NOT NULL,
        FOREIGN KEY (fid) REFERENCES first(fid)
    );
    CREATE TABLE IF NOT EXISTS fifth (
        fiid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
        start_date date NOT NULL,
        end_date date NOT NULL,
        sed int(11) NOT NULL,
        FOREIGN KEY (sed) REFERENCES second(sed),
        fid int(11) NOT NULL,
        FOREIGN KEY (fid) REFERENCES first(fid)
    );
    CREATE TABLE IF NOT EXISTS sixth (
        sid int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
        start_date date NOT NULL,
        end_date date NOT NULL,
        sed int(11) NOT NULL,
        FOREIGN KEY (sed) REFERENCES second(sed),
        fid int(11) NOT NULL,
        FOREIGN KEY (fid) REFERENCES first(fid)
    );
    
    
    //As you can see above, I want to create a single query to query all data at the samee time i.e
    //All table from third table depend on first and second table, but the second table have column firstname and the first table has the p_name column
    
    //I want 
    SELECT second.*, third.* FROM second INNER JOIN third ON third.sed = second.sed
    SELECT second.*, fourth.* FROM second INNER JOIN fourth ON fourth.sed = second.sed
    SELECT second.*, fifth.* FROM second INNER JOIN fifth ON fifth.sed = second.sed
    SELECT second.*, sixth.* FROM second INNER JOIN sixth ON sixth.sed = second.sed
    
    ....WHERE fid = 1;
    

我想将这些查询组合成一个查询,即 $newqueries = '.....';

这个概念

第二张表用于携带所有详细信息,即学生详细信息,但第三到第六表是很少有不同详细信息的表,但它们从第二张表中获取了所有其他详细信息,即学生可以是主席、秘书和副秘书,但不是所有的学生,所以我把他们分类在第三到第六表。第一张表用于保留关于即类的少量信息,所以我想根据类表来区分主席等,但他们都是学生

简而言之

主席、秘书和副秘书是学生,但并非所有学生在一个班级中都有这些角色,但我们有多个班级,如何在一个查询中根据班级区分这些领导

标签: phpmysqlsql

解决方案


您可以使用左连接

SELECT second.*, third.*,fourth.*,fifth.*,sixth.* FROM second 
LEFT JOIN third ON third.sed = second.sed
LEFT JOIN fourth ON fourth.sed = second.sed
LEFT JOIN fifth ON fifth.sed = second.sed
LEFT JOIN sixth ON sixth.sed = second.sed
    
    WHERE second.fid = 1;

我假设如果学生是主席,那么第三张桌子上就会有那个学生的条目。如果学生是普通学生,上述查询将返回 null。如果您也想要角色,也可以使用 CASE 语句。例如,

CASE WHEN third.startdate IS NULL THEN '' ELSE 'Chairman' END

推荐阅读