Solved

Help creating new column in SQL query

Posted on 2011-09-23
1
362 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
ID: 36589183
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Error Code 2 19
Add different cell to otherwise similiar row 4 37
Sql server function help 15 26
MySQL Query Using Up Memory 6 21
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

12 Experts available now in Live!

Get 1:1 Help Now