Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL - help with concat Select statement

Posted on 2011-09-16
4
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
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 Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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

671 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