Solved

Help with SQL statement combining two statements

Posted on 2011-09-23
5
395 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:HainKurt
Comment Utility
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:HainKurt
Comment Utility
can you please give sample data
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
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:HainKurt
Comment Utility
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
Comment Utility
Thanks, I'm going to try to reword the questions as I think I simplified it too much.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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