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 ?
13 6158
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: -
SELECT
-
*
-
FROM
-
(
-
`table111`
-
LEFT JOIN
-
`table222`
-
AS `t2`
-
USING(`item1`)
-
LEFT JOIN
-
`table333`
-
USING(`item1`)
-
)
-
WHERE
-
`t2`.`item2` = 'something'
-
GROUP BY
-
`t2`.`item1`;
-
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 :)
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: -
SELECT
-
DISTINCT
-
*
-
FROM
-
(
-
(
-
SELECT
-
`item1`,
-
`item2`,
-
`item3`
-
FROM
-
`table111`
-
WHERE
-
`item2` = 'something'
-
)
-
UNION
-
(
-
SELECT
-
`item1`,
-
`item2`,
-
`item3`
-
FROM
-
`table222`
-
WHERE
-
`item2` = 'something'
-
)
-
UNION
-
(
-
SELECT
-
`item1`,
-
`item2`,
-
`item3`
-
FROM
-
`table333`
-
WHERE
-
`item2` = 'something'
-
)
-
)
-
GROUP BY
-
`item1`
-
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.
I've got an error : ( here's the exact sql i'm getting: -
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 ?
Heya, coool.
You're missing a quote mark.
No ! that was just a typo when I've copied and paste to here -
SELECT DISTINCT * FROM ((SELECT item1,item2 FROM table1 WHERE item3='4' ) UNION (SELECT item1,item2 FROM table2 WHERE item3='4' )) GROUP BY item2
-
ERROR reported:
Every derived table must have its own alias
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 ? : (
Heya, coool.
Sounds like in your main query, you want to use a SUM(): -
SELECT
-
SUM(`count`)
-
FROM
-
(
-
(
-
SELECT
-
*
-
FROM
-
(
-
SELECT
-
COUNT(`fieldname`)
-
AS `count`
-
FROM
-
$table1
-
GROUP BY
-
$groupBy
-
)
-
AS $table1
-
)
-
UNION
-
(
-
SELECT
-
*
-
FROM
-
(
-
SELECT
-
COUNT(`fieldname`)
-
AS `count`
-
FROM
-
$table2
-
GROUP BY
-
$groupBy
-
)
-
AS $table2
-
)
-
)
-
AS
-
MainTable
-
GROUP BY
-
$groupBy
-
Heya pbmods :)
I've used your structure with some changes - have a look and then read the problem i'm getting ! -
$sql = "
-
SELECT $col1,SUM($col2) FROM
-
(
-
(SELECT * FROM
-
(SELECT $col1, COUNT($col2) AS $col2 FROM $table1 GROUP BY $col1)
-
AS $table1)
-
-
UNION
-
-
(SELECT * FROM
-
(SELECT $col1, COUNT($col2) AS $col2 FROM $table2 GROUP BY $col1)
-
AS $table1)
-
)
-
AS MainTable GROUP BY $col1";
-
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 ?
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.
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
Heya, coool.
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |