简体   繁体   English

如何将这些查询与另一个父表中的 where 子句组合成单个查询?

[英]How can i combine these queries into single query with where clause from another parent table?

How can I combine these queries into a single query with where clause from another parent table?如何将这些查询与另一个父表中的 where 子句组合成一个查询? Please consider my SQL code and suggest a better method to work with请考虑我的 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;
    

I want to combine these queries into a single query ie, $newqueries = '.....';我想将这些查询组合成一个查询,即 $newqueries = '.....';

The concept这个概念

The second table is used to carry all details, ie student details, but the third to sixth tables are tables with few different details but they took all other details from the second table, ie a student can be a chairman, secretary and vice secretary but not all students so that I classified them in third to sixth table.第二张表用于携带所有详细信息,即学生详细信息,但第三到第六表是几乎没有不同详细信息的表,但它们从第二张表中获取了所有其他详细信息,即学生可以是主席、秘书和副秘书,但不是所有的学生,所以我把他们分类在第三到第六表。 The first table used to keep few info about ie classes so I want to differentiate chairman etc base on class tables but all of them are students第一张表用于保留有关 ie 课程的少量信息,所以我想根据 class 表来区分主席等,但他们都是学生

In short简而言之

A chairman, secretary and vice secretary are students but not all students have these role in a class but we have more than one classes, how to differentiate these leaders based on class in a single query主席、秘书和副秘书是学生,但并非所有学生都在 class 中担任这些角色,但我们有不止一个课程,如何在单个查询中根据 class 区分这些领导者

You can use left join您可以使用左连接

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;

I assume that if student is chairman then there will be an entry of that student in third table.我假设如果学生是主席,那么第三张桌子上就会有那个学生的条目。 Above query will return null if the student is normal student.如果学生是普通学生,上述查询将返回 null。 You can use CASE Statement if you want role as well.如果您也想要角色,也可以使用 CASE 语句。 For example,例如,

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

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

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