Solved

Help creating new column in SQL query

Posted on 2011-09-23
1
363 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
[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
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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