Solved

mysql union

Posted on 2014-09-03
6
253 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now