Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL - help with concat Select statement

Posted on 2011-09-16
4
Medium Priority
?
286 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

877 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