Jump to content

Recommended Posts

//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 single query ie, $newqueries = '.....';

 

Link to post
Share on other sites

Why would you want to do that?

Table third, fourth, fifth and sixth all have identical structures - combine into a single table with atype identifier column. It will make life much simpler.

image.thumb.png.957f4fde3d4540f1e4c1e35f585f736f.png

becomes

image.png.53cafd7704ebcd79ead26bfd6a7f0e30.png

Edited by Barand
  • Great Answer 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.