Solved

mysql union

Posted on 2014-09-03
6
254 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 108

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 6

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 6

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 108

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 6

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 108

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Creating and Managing Databases with phpMyAdmin in cPanel.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now