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 > PC based Database Applications > Other PC Databases > How do I get this SELECT statement working?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-08, 09:27
lizardman lizardman is offline
Registered User
 
Join Date: Nov 2008
Posts: 2
How do I get this SELECT statement working?

Hi,

I'm fairly new to SQL, at least to the more complex statements. I'm not really sure how to call the problem I got but please let me explain ...

I got several tables in my (sqlite) Db, some of them contain just lists of names that may be related to in other tables. For example:

companies table:
Code:
id    INTEGER
name  STRING
Now in another table there 'might' be a reference to an id in the companies table but it can also be NULL. e.g. ...

products table:
Code:
id           INTEGER
name         STRING
publisherID  INTEGER
developerID  INTEGER
In the products table entries might have a publisherID or a developerID or even both or they can also both be NULL.

What I'm trying to get with SELECT is a row of a single product that contains all the information from the products table plus the name of the publisher and developer (if any). e.g. ...

Code:
id     name     publisher      developer
-------------------------------------------
0      Foo      companyX       companyY
... or if there is no publisher id for an item in the product table ...

Code:
id     name     publisher      developer
-------------------------------------------
22     Toast    NULL           companyXY

Is there a way to write a SELECT statement that could filter like that or is my approach even elementally wrong here? I've tried several approaches but it ever only lists entries if there is any id for companies in the products table. I haven't found any solution anywhere else so far, not even in some books. I'd appreciate any help!
Reply With Quote
  #2 (permalink)  
Old 11-27-08, 13:46
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
have a look at the JOIN verb, using a LEFT or RIGHT JOIN as required
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 11-27-08, 16:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
always use LEFT OUTER JOIN, always shun RIGHT OUTER JOIN

and healdem, he's using SQLite, not MySQL (i've moved the thread)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-28-08, 00:19
lizardman lizardman is offline
Registered User
 
Join Date: Nov 2008
Posts: 2
So Left Outer Joins are the way to go. Thanks for pointing into the right direction! I get it working now!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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