Jump to content

Join 2 "mysql_query" results


paengski13

Recommended Posts

Hi all!

 

Is it possible to join the 2 mysql_query results before doing the mysql_fetch_assoc.

 

Here is the scenario.

 

Table: School_A_Tbl

student_id          student_name              date_enrolled

A00                          Anna                              March 20

A01                          Ben                                March 22

A02                          Carla                              March 24

 

 

Table: School_B_Tbl

student_id          student_name              date_enrolled

B00                          Dan                              March 21

B01                          Erl                                March 23

B02                          Fox                              March 25

 

Since there is no relationship with the 2 tables I want to combine them order by the date_enrolled

 

$resultA = mysql_query("SELECT * FROM School_A_Tbl");

$resultB = mysql_query("SELECT * FROM School_B_Tbl");

 

What I want to do is combine this data according to date_enrolled. It should be something like this.

 

student_id          student_name              date_enrolled

A00                          Anna                            March 20

B00                          Dan                              March 21

A01                          Ben                              March 22

B01                          Erl                                March 23

A02                          Carla                            March 24

B02                          Fox                              March 25

 

It would be a better help if $resultA and $resultB will be merged before I iterate it using mysql_fetch_assoc.

 

Thanks in advance.

Link to comment
Share on other sites

union might be a better choice here, however you will need to add ALL in your clause, as to not delete duplicate rows.

SELECT * FROM School_A_Tbl UNION ALL SELECT * FROM School_B_Tbl 

for more information on the UNION syntax/usage...visit here

 

Edit: Also, if you are going to use an ORDER BY clause to ORDER the entire UNION result, you will need to parenthesis the individual select statements and place the ORDER BY or LIMIT statements after the last one...e.g

(SELECT * FROM School_A_Tbl) 
UNION ALL 
(SELECT * FROM School_B_Tbl)
ORDER BY col_name DESC|ASC

Link to comment
Share on other sites

Just 1 more question, is it possible to used UNION with different number of columns.

 

I got this error stating "[Err] 1222 - The used SELECT statements have a different number of columns"

 

Is there any other option for this?

 

Ops I think I already found the answer, all I need to do is add NULL to the other table so that the number of columns will matched.

 

(SELECT column1, column2 FROM School_A_Tbl)

UNION ALL

(SELECT column1, NULL FROM School_B_Tbl)

 

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.