Solved

MySQL - help with concat Select statement

Posted on 2011-09-16
4
275 Views
Last Modified: 2012-06-27
I have a table like this:

home_nation        company_nation
CANADA
USA
                             CANADA
                             BERMUDA
                             IRELAND
CANADA              CANADA
                             CANADA
BERMUDA             BERMUDA
BERMUDA
                             USA
BERMUDA
DENMARK             DENMARK

I want to return only unique values from both fields, my current SQL statement looks like this:
SELECT DISTINCT (CONCAT( home_nation, company_nation ))uniqFROM jos_users

Which is very close. It is returning only unique values, but when both fields have a value (like CANADA/CANADA) then the results returned has the country repeated twice, so the current results look like this:

uniq
CANADA
USA
BERMUDA
IRELAND
CANADACANADA
CANADA
BERMUDABERMUDA
DENMARKDENMARK

Is there a way to deal with the double entry problem?

Thanks
     
0
Comment
Question by:elliottbenzle
  • 2
4 Comments
 
LVL 4

Expert Comment

by:ute_arbeit
ID: 36550498
Hi,

if the empty fields are NULL then the statement would be:

SELECT IFNULL(home_nation,company_nation) FROM jos_users;

Cheers
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36552689
IF I'm understanding correctly .. this will return the unique nations in either column

   SELECT DISTINCT home_nation AS uniq  FROM jos_users
   UNION
   SELECT DISTINCT company_nation AS uniq  FROM jos_users

Just exclude the empty values. I'm assuming an empty string.  If not adjust the filter accordingly


SELECT t.Uniq
FROM   (
           SELECT DISTINCT home_nation AS uniq  FROM jos_users
           UNION
           SELECT DISTINCT company_nation AS uniq  FROM jos_users
      ) t
WHERE t.Uniq <> ''



   SELECT DISTINCT home_nation AS uniq  FROM jos_users
   UNION
   SELECT DISTINCT company_nation AS uniq  FROM jos_users
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 36552691
Sorry. ignore the last 3 lines. it's a copy paste error.  The final query is just:

SELECT t.Uniq
FROM   (
           SELECT DISTINCT home_nation AS uniq  FROM jos_users
           UNION
           SELECT DISTINCT company_nation AS uniq  FROM jos_users
      ) t
WHERE t.Uniq <> ''

Open in new window

0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 36564508
Thanks. That was very helpful.
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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

18 Experts available now in Live!

Get 1:1 Help Now