473,441 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Union/Merge more than one query !

67
Hi

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

example:
Expand|Select|Wrap|Line Numbers
  1. SELECT item1,item2,item3 FROM table111 WHERE item2='something' GROUP BY item1
  2. UNION
  3. SELECT item1,item2,item3 FROM table222 WHERE item2='something' GROUP BY item1
  4. UNION
  5. 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 ?
Aug 9 '07 #1
13 6158
pbmods
5,821 Expert 4TB
Heya, coool. Welcome to TSDN!

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

I think what you are trying to do is join your tables:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         *
  3.     FROM
  4.     (
  5.             `table111`
  6.         LEFT JOIN
  7.             `table222`
  8.                 AS `t2`
  9.                 USING(`item1`)
  10.         LEFT JOIN
  11.             `table333`
  12.                 USING(`item1`)
  13.     )
  14.     WHERE
  15.         `t2`.`item2` = 'something'
  16.     GROUP BY
  17.         `t2`.`item1`;
  18.  
Aug 9 '07 #2
coool
67
Thanks for the nice way you've advised me to use the tags.. :)

do you think the mysql code you've wrote will give the same result of the mysql code I've written above ?

maybe if you explained for me what your code is doing, I can tell if that's really what I'm looking for.. will you do that please ?

thanks in advance,

coool :)
Aug 9 '07 #3
pbmods
5,821 Expert 4TB
Heya, coool.

I'm not entirely certain what you're trying to accomplish.

The nice thing about using the UNION keyword is that it allows you to return a single result set for multiple queries. The issue with doing it this way is that it is in essence just three separate queries that don't talk to each other.

In my previous post provided an example of a join as one way to approach the situation. It's different than using the UNION keyword because instead of running three separate queries, it runs one query across all three tables looking for similar records.

However, at second glance, you might find it more useful just to use a subquery:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     DISTINCT
  3.         *
  4.     FROM
  5.     (
  6.         (
  7.             SELECT
  8.                     `item1`,
  9.                     `item2`,
  10.                     `item3`
  11.                 FROM
  12.                     `table111`
  13.                 WHERE
  14.                     `item2` = 'something'
  15.         )
  16.         UNION
  17.         (
  18.             SELECT
  19.                     `item1`,
  20.                     `item2`,
  21.                     `item3`
  22.                 FROM
  23.                     `table222`
  24.                 WHERE
  25.                     `item2` = 'something'
  26.         )
  27.         UNION
  28.         (
  29.             SELECT
  30.                     `item1`,
  31.                     `item2`,
  32.                     `item3`
  33.                 FROM
  34.                     `table333`
  35.                 WHERE
  36.                     `item2` = 'something'
  37.         )
  38.     )
  39.     GROUP BY
  40.         `item1`
  41.  
This query runs the three UNION'ed queries and then lumps them together into a single result set. Then in runs through and pulls out only the rows that have a unique value in the `item1` field.
Aug 9 '07 #4
coool
67
I've got an error : (

here's the exact sql i'm getting:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT * FROM ((SELECT item1,item2 FROM table1 WHERE item3='4' ) UNION (SELECT item1,item2 FROM table2 WHERE item3='4 )) GROUP BY item2
here's the error:
Every derived table must have its own alias

do you have any idea why this is happening ! ..what's the solution ?
Aug 9 '07 #5
pbmods
5,821 Expert 4TB
Heya, coool.

You're missing a quote mark.
Aug 9 '07 #6
coool
67
No ! that was just a typo when I've copied and paste to here

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT * FROM ((SELECT item1,item2 FROM table1 WHERE item3='4' ) UNION (SELECT item1,item2 FROM table2 WHERE item3='4' )) GROUP BY item2
  2.  
ERROR reported:
Every derived table must have its own alias
Aug 9 '07 #7
pbmods
5,821 Expert 4TB
Heya, coool.

Have a look at this comment.
Aug 9 '07 #8
coool
67
I've read the comments.. they're very useful

but things didn't work until i added names to my sub-tables

anyway, now i've a new problem with this

I'm getting the most biggest count from multible tables

this is what i'm using:
[php]
$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";
[/php]

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 ? : (
Aug 10 '07 #9
pbmods
5,821 Expert 4TB
Heya, coool.

Sounds like in your main query, you want to use a SUM():

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         SUM(`count`)
  3.     FROM
  4.     (
  5.         (
  6.             SELECT
  7.                     *
  8.                 FROM
  9.                 (
  10.                     SELECT
  11.                             COUNT(`fieldname`)
  12.                                 AS `count`
  13.                         FROM
  14.                             $table1
  15.                         GROUP BY
  16.                             $groupBy
  17.                 )
  18.                 AS $table1
  19.         )
  20.         UNION
  21.         (
  22.             SELECT
  23.                     *
  24.                 FROM
  25.                 (
  26.                     SELECT
  27.                             COUNT(`fieldname`)
  28.                                 AS `count`
  29.                         FROM
  30.                             $table2
  31.                         GROUP BY
  32.                             $groupBy
  33.                 )
  34.                 AS $table2
  35.         )
  36.     )
  37.     AS
  38.         MainTable
  39.     GROUP BY
  40.         $groupBy
  41.  
Aug 10 '07 #10
coool
67
Heya pbmods :)

I've used your structure with some changes - have a look and then read the problem i'm getting !

Expand|Select|Wrap|Line Numbers
  1. $sql = "
  2. SELECT $col1,SUM($col2) FROM
  3. (
  4.    (SELECT * FROM
  5.      (SELECT $col1, COUNT($col2) AS $col2 FROM $table1 GROUP BY $col1)
  6.    AS $table1)
  7.  
  8.    UNION
  9.  
  10.    (SELECT * FROM
  11.      (SELECT $col1, COUNT($col2) AS $col2 FROM $table2 GROUP BY $col1)
  12.    AS $table1)
  13. )
  14. AS MainTable GROUP BY $col1";
  15.  
Result of table1 query is:
item1 = 20
item2 = 5
item3 = 1

Result of table2 query is:
item1 = 40
item2 = 0 - because it's zero it doesn't appear that's okay
item3 = 1

Result of MainTable query is:
item1 = 60
item2 = 5
item3 = 1

here's the problem in item3 :(

I'm getting the sum is 1, where it should be 2

by the way,
table1.item3 is 1234
table2.item3 is 1234-1

so the difference is just (-1)

what do you think of that ?
Aug 10 '07 #11
pbmods
5,821 Expert 4TB
Heya, coool 8)

In your query, you're only selecting $col1 and $col2, so there shouldn't be an item3 field in any of your result sets.

by the way,
table1.item3 is 1234
table2.item3 is 1234-1

so the difference is just (-1)

what do you think of that ?
I think you're crazy. Heh. Seriously now, I think what you posted is one step behind where you are.
Aug 10 '07 #12
coool
67
my query is correct

and by item1,2,3 I mean the ouput !!

I just needed to use UNION ALL instead of UNION

and now it's working fine :)

thanks for your help
Aug 10 '07 #13
pbmods
5,821 Expert 4TB
Heya, coool.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 10 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
14
by: Salad | last post by:
A97. Situation: I have 3 tables with a text field in each and a date field in the first 2 tables: Table1 Text1, Date1 Table2 Text2, Date2 Table3 Text3 (no date field) The following...
8
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for...
5
by: Lyn | last post by:
This one is difficult to explain, so I will cut it down to the basics. I have a major table 'tblA' which has an autonum field 'ID-A' as primary key (of no significance to users). 'tblA' contains...
12
by: Susan Bricker | last post by:
For those of you who have been following my posts - they all pertain to a Dog Competition Organization's Database. There are three classes that the dogs can participate: NOVICE, OPEN, and...
3
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are...
5
by: j0mbolar | last post by:
The following was labeled as invoking undefined behavior but I don't see that happening at all: struct ints { int a; int b; }; union merge { float f;
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.