Solved

Help creating new column in SQL query

Posted on 2011-09-23
1
357 Views
Last Modified: 2012-05-12
I am creating a SELECT query

I have a table with a lot of columns, I need to take two of these columns and merge them into a new column within the query. These two columns are 'home_city' and 'company_city' I want to merge these two columns into a new column called 'city'. The merging statement looks like this:

SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users

This is what the new column cities will look like.

And I want to insert that into the below Query:

SELECT u.name AS name, u.username AS username, u.person_id AS person_id, u.userid AS userid, u.email AS email, u.title AS title, u.company_name AS company_name, u.company_addr1 AS company_addr1, u.company_addr2 AS company_addr2, u.company_city AS company_city, u.company_state AS company_state, u.company_zip AS company_zip, u.company_nation AS company_nation, u.work_phone1 AS work_phone1, u.phone_ext AS phone_ext, u.fax_number AS fax_number, u.hide_home_addr AS hide_home_addr, u.home_addr1 AS home_addr1, u.home_addr2 AS home_addr2, u.home_city AS home_city, u.home_state AS home_state, u.home_zip AS home_zip, u. home_nation AS home_nation, u.home_phone AS home_phone, u.hide_display AS hide_display, g.name AS usertype FROM #__users AS u INNER JOIN #__core_acl_aro AS aro ON aro.value = u.id INNER JOIN #__core_acl_groups_aro_map AS gm ON gm.aro_id = aro.id INNER JOIN #__core_acl_aro_groups AS g ON g.id = gm.group_id WHERE (u.person_id LIKE '%a%' AND u.userid LIKE '%a%' AND u.home_nation LIKE '%USA%' AND u.home_nation LIKE '%USA%' AND (u.home_city LIKE '%%' OR u.company_city LIKE '%%') AND (u.home_state LIKE '%OH%' OR u.company_state LIKE '%OH%')) AND g.name = 'Registered' ORDER BY name ASC, u.id ASC LIMIT 0, 50

The above query currently returns 47 columns. I want the new query to return 48 columns. The new column will be called 'city' and will contain the value of 'SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users'

My initial attempt is below, but it gives an error: #1242 - Subquery returns more than 1 row

My initial attempt (addition is on second line):
SELECT
 (SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users) AS city,
u.name AS name, u.username AS username, u.person_id AS person_id, u.userid AS userid, u.email AS email, u.title AS title, u.company_name AS company_name, u.company_addr1 AS company_addr1, u.company_addr2 AS company_addr2, u.company_city AS company_city, u.company_state AS company_state, u.company_zip AS company_zip, u.company_nation AS company_nation, u.work_phone1 AS work_phone1, u.phone_ext AS phone_ext, u.fax_number AS fax_number, u.hide_home_addr AS hide_home_addr, u.home_addr1 AS home_addr1, u.home_addr2 AS home_addr2, u.home_city AS home_city, u.home_state AS home_state, u.home_zip AS home_zip, u. home_nation AS home_nation, u.home_phone AS home_phone, u.hide_display AS hide_display, g.name AS usertype FROM jos_users AS u INNER JOIN jos_core_acl_aro AS aro ON aro.value = u.id INNER JOIN jos_core_acl_groups_aro_map AS gm ON gm.aro_id = aro.id INNER JOIN jos_core_acl_aro_groups AS g ON g.id = gm.group_id WHERE (u.person_id LIKE '%a%' AND u.userid LIKE '%a%' AND u.home_nation LIKE '%USA%' AND u.home_nation LIKE '%USA%' AND (u.home_city LIKE '%%' OR u.company_city LIKE '%%') AND (u.home_state LIKE '%OH%' OR u.company_state LIKE '%OH%')) AND g.name = 'Registered' ORDER BY name ASC, u.id ASC LIMIT 0, 50

Any help would be appreciated.

Thanks
0
Comment
Question by:elliottbenzle
1 Comment
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
Hi.

If I understand you correctly and you want an additional column; however, it is not clear what you actually want in that column as you are already displaying home_city and company_city as columns in the original 47. Your UNION will return a list of the distinct cities between both the home_city and company_city field. Therefore, that is why the error states there is more than one row in the result.

What is the intent of this new column?

Could you be trying to set City to the NON-NULL value?

e.g.
COALESCE(u.home_city, u.company_city) AS city

Let me know.

Kevin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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