Solved

MySQL - help with concat Select statement

Posted on 2011-09-16
4
279 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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