Solved

Help with SQL statement combining two statements

Posted on 2011-09-23
5
400 Views
Last Modified: 2012-05-12
I have a table with two columns 'home_city' & 'company_city'

During my select Query I want to take these two fields and combine them into one field called 'city'

My current SQL statement looks like this:
SELECT u.company_city AS company_city, u.home_city AS home_city FROM jos_users AS u ORDER BY city ASC, u.id ASC LIMIT 0, 50

and the Query I use to create the new 'city' field would look like this:
SELECT DISTINCT home_state city FROM jos_users UNION SELECT DISTINCT company_state FROM jos_users

I'm not sure how to combine the two, but I think it would look something like this:

SELECT  (SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users) AS city FROM jos_users AS u ORDER BY city ASC, u.id ASC LIMIT 0, 50

but this gives me an error: #1242 - Subquery returns more than 1 row

How do I do this properly?

Thanks

0
Comment
Question by:elliottbenzle
  • 4
5 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36588450
use just this

select
home_city + company_city as city
FROM jos_users AS u ORDER BY city ASC, u.id ASC LIMIT 0, 50
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36588457
can you please give sample data
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 36588466
maybe you want this:

SELECT DISTINCT city
FROM (
SELECT DISTINCT home_city city FROM jos_users
UNION
SELECT DISTINCT company_city FROM jos_users
) AS Cities
ORDER BY city ASC LIMIT 0, 50
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36588475
or this (may not need another select)

SELECT home_city city FROM jos_users
UNION
SELECT company_city FROM jos_users
ORDER BY city ASC LIMIT 0, 50
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 36588728
Thanks, I'm going to try to reword the questions as I think I simplified it too much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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