1

I am a bit of a newbie at SQL (and I apologize if this has been answered elsewhere although I can't seem to find anything that nails it) and have been stumped for ages on this problem. As a foreword, I have researched and know about various joins and inner joins etcetera and I understand how they work and they seem to work well for smaller amounts of data. However, my problem is that the number of columns and number of tables is so large, I need to know if there is a simpler way to join this data into one query.

So this is what I have:

 $query2 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase1 WHERE id IN(3,12,22)";
    $query3 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase2 WHERE id IN(3,12,22)";
    $query4 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase3 WHERE id IN(3,12,22)";
    $query5 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase4 WHERE id IN(3,12,22)";
    $query6 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase5 WHERE id IN(3,12,22)";
    $query7 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase6 WHERE id IN(3,12,22)";
    $query8 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase7 WHERE id IN(3,12,22)";
    $query9 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase8 WHERE id IN(3,12,22)";
    $query10 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase9 WHERE id IN(3,12,22)";
    $query11 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase10 WHERE id IN(3,12,22)";
    $query12 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase11 WHERE id IN(3,12,22)";
    $query13 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase12 WHERE id IN(3,12,22)";

I would like to combine these query results into one query. I have tried a simple comma separation between phase1, phase2 e.t,c, but I am thrown an ambiguous column error. I understand this is due to the fact that I did not add

phase1.fen_toit

OR

phase1.tuiles

and so on to the SELECT, as this seems ridiculously long!

If anyone could help I would be extremely grateful. I am sure the answer is staring me in the face!

3
  • So next see normalization Commented Feb 6, 2015 at 0:19
  • Use UNION to combine them. Commented Feb 6, 2015 at 0:21
  • You can also use the MERGE storage engine to create a table that automatically performs a UNION between all these tables. Commented Feb 6, 2015 at 0:22

1 Answer 1

1

Join your queries using UNION or UNION ALL which looks like below:

$query2 = "SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase1 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase2 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase3 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase4 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase5 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase6 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase7 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase8 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase9 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase10 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase11 WHERE id IN(3,12,22)
             UNION ALL
           SELECT fen_toit,tuiles,zinc,bois,bac_acier FROM phase12 WHERE id IN(3,12,22)";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.