Solved

mysql union

Posted on 2014-09-03
6
255 Views
Last Modified: 2014-09-04
When I use a mysql union my results are not being combined.

        (
         SELECT
            LOWER(`countries`.`country`) AS 'country',
            COUNT(LOWER(`countries`.`country`)) AS 'total',
            COUNT(CASE WHEN DATE_FORMAT(`charters`.`start_date`, '%Y') = '$year1' THEN `charters`.`start_date` END) AS '$year1',
            COUNT(CASE WHEN DATE_FORMAT(`charters`.`start_date`, '%Y') = '$year2' THEN `charters`.`start_date` END) AS '$year2',
            COUNT(CASE WHEN DATE_FORMAT(`charters`.`start_date`, '%Y') = '$year3' THEN `charters`.`start_date` END) AS '$year3',
            COUNT(CASE WHEN DATE_FORMAT(`charters`.`start_date`, '%Y') = '$year4' THEN `charters`.`start_date` END) AS '$year4',
            COUNT(CASE WHEN DATE_FORMAT(`charters`.`start_date`, '%Y') = '$year5' THEN `charters`.`start_date` END) AS '$year5'

....

     )
     UNION
     (
         SELECT
            LOWER(`db2`.`countries`.`name`) AS 'country',
            COUNT(LOWER(`db2`.`countries`.`name`)) AS 'total',
            COUNT(CASE WHEN DATE_FORMAT(`db2`.`charters`.`begin_date`, '%Y') = '$year1' THEN `db2`.`charters`.`begin_date` END) AS '$year1',
            COUNT(CASE WHEN DATE_FORMAT(`db2`.`charters`.`begin_date`, '%Y') = '$year2' THEN `db2`.`charters`.`begin_date` END) AS '$year2',
            COUNT(CASE WHEN DATE_FORMAT(`db2`.`charters`.`begin_date`, '%Y') = '$year3' THEN `db2`.`charters`.`begin_date` END) AS '$year3',
            COUNT(CASE WHEN DATE_FORMAT(`db2`.`charters`.`begin_date`, '%Y') = '$year4' THEN `db2`.`charters`.`begin_date` END) AS '$year4',
            COUNT(CASE WHEN DATE_FORMAT(`db2`.`charters`.`begin_date`, '%Y') = '$year5' THEN `db2`.`charters`.`begin_date` END) AS '$year5'

   ...
   )
  ORDER BY `country` ASC

Open in new window


However, what is happening is this:
Country, Total, 2010, 2011, 2012, 2013, 2014
australia       29       5       13       11       0       0
australia       30       0       0       0       7       23
austria       3       2       1       0       0       0
austria       4       0       0       0       1       3
belgium       10       3       2       5       0       0
belgium       8       0       0       0       3       5

I want each to sum together.
0
Comment
Question by:Robert Saylor
  • 3
  • 3
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40301587
Not sure exactly what you want to get here, but when you want combined results the right clause is often GROUP BY.
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40301594
In both my select statements I am using a group by...

(select ... where ... group by country) union (select ... where ... group by country) order by country

I have to use a union because I am pulling similar data from two different databases.
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40301797
I ended up putting the data into an array then was able to combine data-sets. But I still don't understand why the union returned the data un grouped even though it was grouped.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40302369
I must be missing something.  I copied your code snippet and scanned it for GROUP before posting my answer.  It does not contain GROUP, so I understood that GROUP was not part of the queries.  It's really helpful to have the SSCCE when you've got a question like this one.  We would want CREATE TABLE statements, a few lines of data to load into the tables, and an example of the data you wanted to find in the query results set.  When you can give us that information, you can almost always get a quick and accurate answer.  And when we have less than the SSCCE, it just takes us longer to "guess" about what you have and what you want to achieve.  And that means wasted time for you.

As it stands now, I certainly do not understand the query you're using, having never seen the GROUP clause, but if you've got a query + PHP that gives you the right answers that's probably good enough.  A good programmer recognizes good programming.  A great programmer recognizes good-enough programming!

Best of luck with your project, ~Ray
0
 
LVL 7

Author Closing Comment

by:Robert Saylor
ID: 40303650
didn't want to post the entire query to protect information but with PHP I was able to get the job done.

A great programmer recognizes good-enough programming. That's your answer and it's good for me :)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40303716
FWIW, I often use PHP to do things that I could do in MySQL.  For one thing, the code has to run somewhere, so there is not really any great savings in moving the code to the database engine.  And for another, I'm smarter about PHP than I am about MySQL, so there is personal prejudice involved :-)

Thanks for the points and best of luck with it, ~Ray
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysqli Query 5 52
Install MySQL 5.6 and PHP on Centos Linux 6 120
MySQL left join performance 4 38
updating the date data 12 30
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question