Jump to content

Recommended Posts

I've got a table with a list of cities, and a table with people. The cities table has a column for a manager

 

I want all the people from the cities managed by a certain person

 

Such as Select * from cities where manager = 'joe'

 

Then use that to select * from people where city = all the cities from the other query

 

Can I do this in one step or do I need to select the cities, put them in an array and loop through them while selecting the people?

 

I'm using PG but any general help is welcome

Using IN is like a multiple WHERE clauses. Example:

SELECT *
FROM users
WHERE favoriteColor IN ('blue', 'green', 'red')

That is logically the same as

SELECT *
FROM users
WHERE favoriteColor ==  'blue'
  OR favoriteColor ==  'green'
  OR favoriteColor ==  'red'

 

In addition to using a comma separated list of values for the IN values you can also do a query. So, I used a query to get all the city values for the selected managers. The result is a query that grabs all the people records where their city is in the list of cities for the selected manager.

 

The second query uses a simple JOIN. If you don't know how to use JOINs you need to learn. Without using JOINs you will be increasing your coding work and - most likely - the system overhead.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.