Jump to content

Union/Merge more than one query !


coool

Recommended Posts

Hi

 

How can I merge the result of few quries that had been unioned ?

 

example:

SELECT item1,item2,item3 FROM table111 WHERE item2='something' GROUP BY item1

UNION

SELECT item1,item2,item3 FROM table222 WHERE item2='something' GROUP BY item1

UNION

SELECT item1,item2,item3 FROM table333 WHERE item2='something' GROUP BY item1

 

NOTE:

table111, table222, and table333 has the same structure

always i'm asking for same items to be selected here and for same group by and where statment

the difference is with the table names

each table has different records than the other table

 

Is there any available code to do this ! any solution !

 

do you any idea of how doing this, do you have any simple example ?

Link to comment
Share on other sites

 

I've got one solution but still have one more problem

 

I'm getting the most biggest count from multible tables

 

this is what i'm using:

 

$fields = "Status,Count(names)";
$table1 = "myTable1";
$table2 = "myTable2";
$groupBy = "Status";

$sql = "SELECT * FROM((SELECT * FROM (SELECT $fields FROM $table1 GROUP BY $groupBy)AS $table1) UNION
       (SELECT * FROM(SELECT $fields FROM $table2 GROUP BY $groupBy)AS $table2))AS MainTable GROUP BY $groupBy";

 

so if my first table gives:

status1 = 40 items

status2 = 60 items

 

and if my second table gives:

status1 = 250

status2 = 20

 

the result from the above code is:

status1 = 250

status2 = 60

 

which is not what i'm looking for (it's just outputing the biggest numbers)

 

i want is this answer:

status1 = 290

status2 = 80

 

what do you think ? : (

 

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.