简体   繁体   中英

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? Please consider my SQL code and suggest a better method to work with

//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 = '.....';

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

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

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. You can use CASE Statement if you want role as well. For example,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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