dbTalk Databases Forums  

SELECT when WHERE condition is in another table

comp.databases.mysql comp.databases.mysql


Discuss SELECT when WHERE condition is in another table in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John
 
Posts: n/a

Default SELECT when WHERE condition is in another table - 02-13-2011 , 02:58 AM






Hi

I have a table 'major' containing only two columns 'city' and 'pop'.

I have another table 'country' containing 100 columns.

I want to SELECT from COUNTRY those rows where the city's pop is more than
10000.

SELECT ind,fish,city FROM country WHERE country.city=major.city AND
major.city.pop<10000

I appreciate I cannot say "major.city.pop" but how do I access 'pop' of
'city' in 'major'?

Regards
John

Reply With Quote
  #2  
Old   
Helmut Chang
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 04:37 AM






Am 13.02.2011 09:58, schrieb John:

Quote:
I have a table 'major' containing only two columns 'city' and 'pop'.

I have another table 'country' containing 100 columns.
Which seems to be a broken design to me... Tables with that many columns
almost always indicate some mistakes in the design.

Quote:
I want to SELECT from COUNTRY those rows where the city's pop is more than
10000.

SELECT ind,fish,city FROM country WHERE country.city=major.city AND
major.city.pop<10000
Which confirms my suspicion about the broken design: A city is not an
attribute of a country, it's the other way round. Except, the city means
the capital here...

Quote:
I appreciate I cannot say "major.city.pop" but how do I access 'pop' of
'city' in 'major'?
Why 'pop' of 'city'? You said, your table is 'major', containing only
two columns 'city' and 'pop'. So you access the column 'pop' of 'major':
major.pop

HTH, Helmut

Reply With Quote
  #3  
Old   
John
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 05:34 AM



"Helmut Chang" <usenet (AT) helmutchang (DOT) at> wrote

Quote:
Am 13.02.2011 09:58, schrieb John:

I have a table 'major' containing only two columns 'city' and 'pop'.

I have another table 'country' containing 100 columns.

Which seems to be a broken design to me... Tables with that many columns
almost always indicate some mistakes in the design.

I want to SELECT from COUNTRY those rows where the city's pop is more
than
10000.

SELECT ind,fish,city FROM country WHERE country.city=major.city AND
major.city.pop<10000

Which confirms my suspicion about the broken design: A city is not an
attribute of a country, it's the other way round. Except, the city means
the capital here...

I appreciate I cannot say "major.city.pop" but how do I access 'pop' of
'city' in 'major'?

Why 'pop' of 'city'? You said, your table is 'major', containing only two
columns 'city' and 'pop'. So you access the column 'pop' of 'major':
major.pop

HTH, Helmut
Hi

Thank you for your reply.

My question is how to correct the WHERE statement so that I only extract
those rows where the country.city and the major.city are the same but the
major.pop is less than 10000.

Thank you.

Regards
John
#



>

Reply With Quote
  #4  
Old   
J.O. Aho
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 06:24 AM



John wrote:
Quote:
"Helmut Chang" <usenet (AT) helmutchang (DOT) at> wrote in message
news:4d57b45c$0$18682$91cee783 (AT) newsreader03 (DOT) highway.telekom.at...
Am 13.02.2011 09:58, schrieb John:

I have a table 'major' containing only two columns 'city' and 'pop'.

I have another table 'country' containing 100 columns.

Which seems to be a broken design to me... Tables with that many columns
almost always indicate some mistakes in the design.

I want to SELECT from COUNTRY those rows where the city's pop is more
than
10000.

SELECT ind,fish,city FROM country WHERE country.city=major.city AND
major.city.pop<10000
SELECT c.ind, c.fish, c.city, m.pop
FROM country c
INNER JOIN major m ON(c.city = m.city)
WHERE m.pop > 10000

Quote:
Which confirms my suspicion about the broken design: A city is not an
attribute of a country, it's the other way round. Except, the city means
the capital here...

I appreciate I cannot say "major.city.pop" but how do I access 'pop' of
'city' in 'major'?

Why 'pop' of 'city'? You said, your table is 'major', containing only two
columns 'city' and 'pop'. So you access the column 'pop' of 'major':
major.pop

My question is how to correct the WHERE statement so that I only extract
those rows where the country.city and the major.city are the same but the
major.pop is less than 10000.
SELECT c.ind, c.fish, c.city, m.pop
FROM country c
INNER JOIN major m ON(c.city = m.city)
WHERE m.pop < 10000


But really, see if you can do something about the tables you are using, 100
columns? Naming of the tables should be more propitiate, the table "major"
should maybe been called "city_population" and instead of having the name as
the index, just had an auto_increment index, for example if a city changes
name (Leningrad -> Petersburg), then you don't want to go and change the name
in many tables, you want just to do that in one place.


--

//Aho

Reply With Quote
  #5  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 06:33 AM



On Feb 13, 8:58*am, "John" <john1... (AT) yahoo (DOT) com> wrote:
Quote:
Hi

I have a table 'major' containing only two columns 'city' and 'pop'.

I have another table 'country' containing 100 columns.

I want to SELECT from COUNTRY those rows where the city's pop is more than
10000.

SELECT ind,fish,city FROM country WHERE country.city=major.city AND
major.city.pop<10000

I appreciate I cannot say "major.city.pop" but how do I access 'pop' of
'city' in 'major'?

Regards
John
As others have pointed out, yur schema is fundamentally flawed. For
instance What is to stop 2 countries having cities with the same name?

Reply With Quote
  #6  
Old   
Hans Castorp
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 06:51 AM



Captain Paralytic wrote on 13.02.2011 13:33:
Quote:
What is to stop 2 countries having cities with the same name?
That is not something that should be prevented.

It's not really uncommon, just think about the many U.S. cities that were named after european cities (Berlin, Paris, London, Madrid)

Regards
Thomas

Reply With Quote
  #7  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 07:21 AM



Hans Castorp wrote:
Quote:
Captain Paralytic wrote on 13.02.2011 13:33:
What is to stop 2 countries having cities with the same name?

That is not something that should be prevented.

It's not really uncommon, just think about the many U.S. cities that
were named after european cities (Berlin, Paris, London, Madrid)

I am not sure how many Birminghams or Newcastles there are around the world.


Alabama has one, the UK another.

Newcastle is in South Africa as well as the UK. SA also has an Utrecht,
Wartburg, Worcester, Richmond, Margate, Kempton Park, Bewley, Bedford..

Boston exists in the USA and the UK.


The whole of the post colonial English speaking world is littered with
towns of the same name.


Quote:
Regards
Thomas




Reply With Quote
  #8  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 10:00 AM



On Feb 13, 12:51*pm, Hans Castorp <SOZRBLNTL... (AT) spammotel (DOT) com> wrote:
Quote:
Captain Paralytic wrote on 13.02.2011 13:33:

What is to stop 2 countries having cities with the same name?

That is not something that should be prevented.

It's not really uncommon, just think about the many U.S. cities that werenamed after european cities (Berlin, Paris, London, Madrid)

Regards
Thomas
That is my point. The schema as described cannot handle this situation.

Reply With Quote
  #9  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 10:10 AM



Captain Paralytic:

Quote:
On Feb 13, 12:51*pm, Hans Castorp <SOZRBLNTL... (AT) spammotel (DOT) com> wrote:
Captain Paralytic wrote on 13.02.2011 13:33:

What is to stop 2 countries having cities with the same name?

It's not really uncommon, just think about the many U.S. cities
that were named after european cities (Berlin, Paris, London,
Madrid)

That is my point. The schema as described cannot handle this
situation.
Try and get the village of Baarle into a DB-schema! :-)
http://en.wikipedia.org/wiki/Baarle-Hertog

Oh, and what about Nicosia?


--
Erick

Reply With Quote
  #10  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: SELECT when WHERE condition is in another table - 02-13-2011 , 10:13 AM



Erick T. Barkhuis:

Quote:
Try and get the village of Baarle into a DB-schema! :-)
http://en.wikipedia.org/wiki/Baarle-Hertog
....and in which country is this library situated?
http://en.wikipedia.org/wiki/File:Ha...BorderLine.jpg



--
Erick

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.