If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Trouble with querying

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-17-09, 12:32
mk24 mk24 is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
Trouble with querying

Hello, I am new to SQL. I am currently using PostGres, but i think querying is pretty much the same?

I have the following tables:
common, cds, and position

cds inherits common

common and cds are linked by 'id'.

common has values start and stop.

I want to find all the 'position.target_pos' that lie within a cds region (the start and stop are obtained from common).


I have this query:

Quote:
SELECT count(*) FROM common, cds, position WHERE cds.id = common.id AND position.target_pos > common.start AND position.target_pos < common.stop
however, this is returning me the value of over 1,000 which is impossible if my query is correct because there are only 800 entried in the position table. Therefor, i'm pretty sure my query is wrong, but i'm not sure where!!

Please help!
Reply With Quote
  #2 (permalink)  
Old 08-18-09, 03:55
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 58
Play around with smaller tables and you'll learn how joins work.

If you have two tables, one with 4 rows and one with 5 rows, the join will return 4 * 5 = 20 rows if you have no join condition.

(BTW, maybe you need some more join condition on the positions table.)
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On