Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

SQL help needed.

Thread Tools
 
Search this Thread
 
Old Jul 10, 2003 | 05:43 PM
  #1  
Dracoro's Avatar
Dracoro
Thread Starter
Scooby Regular
 
Joined: Sep 2001
Posts: 10,261
Likes: 0
From: A powerslide near you
Post

Not sure what kcname is??

Anyway, it can be done two ways (if I've interpreted your requirement correctly!!!)

SELECT p.petname, d.petname, s.petname
FROM Dog p, Dog d, Dog s
WHERE p.Dam = d.DogID
AND p.Sire = s.DogID
WHERE p.petname = 'Harry'

SELECT p.petname, d.petname, s.petname
FROM Dog p
INNER JOIN Dog d ON d.DogID = p.Dam
INNER JOIN Dog s ON s.DogID = p.Sire
WHERE p.petname = 'Harry'

If you want to bring back dogs that may or may NOT have dams or sires then make the joins outer ones (OUTER JOIN or (+) syntax). Above will only bring back Harry if he has both a sire and a dam.

The 'WHERE' bit is optional (with it for just Harry's mum & dad) or leave it out to get mum & dads for all records. I've just put it in so it looks more obvious what I'm doing.

HTH.

[Edited by Dracoro - 10/7/2003 7:28:39 PM]
Reply
Old Oct 7, 2003 | 05:32 PM
  #2  
ScoobyWon't's Avatar
ScoobyWon't
Scooby Regular
 
Joined: May 2002
Posts: 16,694
Likes: 0
From: Pot Belly HQ
Arrow

ANy SQL experts want to take a look at this and give me a hint as to how to create the correct syntax. In theory I believe this is a one->many relationship.

(Excuse my attempt at ASCII art)

+-------+
¦Dogs ¦
+-------+
¦DogID ¦
¦Kcname ¦
¦Petname¦
¦Dam ¦
¦Sire ¦
+-------+

I am attempting to output the following:

"Hello my name is <%Petname%> my mother is <%Dam%> and my father is <%Sire%>."

Therefore I need to join the value of the Dam and of the Sire to it's own DogID to give me (Below are example records):

"Hello my name is Harry, my mother is Sally and my father is Barry."

Example records:

DogID: 1
Petname: Harry
Dam: 2
Sire: 3

DogID: 2
Petname: Sally
Dam:
Sire:

DogID: 3
Petname: Barry
Dam:
Sire:

How do I go about creating this query? Am I correct in thinking that I may have to do a Join dog.petname as pup and dog.sire as family.dam. Utterly confused... Help! Please!!!!
Reply
Old Oct 7, 2003 | 05:35 PM
  #3  
Hobo_Jojo's Avatar
Hobo_Jojo
Scooby Regular
 
Joined: Aug 2003
Posts: 1,981
Likes: 0
Post

give up. simple.
Reply
Old Oct 7, 2003 | 09:35 PM
  #4  
ScoobyWon't's Avatar
ScoobyWon't
Scooby Regular
 
Joined: May 2002
Posts: 16,694
Likes: 0
From: Pot Belly HQ
Post

Excellent. Cheers. Got it working by passing the DogID in the end, hadn't thought of the outerjoin for no parents, thanks

Have it working with the following (expanded the criteria to include extra info)

SELECT Dogs.Kcname, Dogs_dam.Kcname, Dogs_sire.Kcname, Dogs.Petname, Breed.Breed, Sex.Sex
FROM ((Dogs AS Dogs_sire INNER JOIN (Dogs INNER JOIN Dogs AS Dogs_dam ON Dogs.Dam = Dogs_dam.DogID) ON Dogs_sire.DogID = Dogs.Sire) INNER JOIN Breed ON Dogs.BreedID = Breed.BreedID) INNER JOIN Sex ON Dogs.SexID = Sex.SexID
WHERE (((Dogs.DogID)=1));
Reply
Old Oct 7, 2003 | 10:18 PM
  #5  
Dracoro's Avatar
Dracoro
Thread Starter
Scooby Regular
 
Joined: Sep 2001
Posts: 10,261
Likes: 0
From: A powerslide near you
Smile

Hope you don't take offence if I give some constructive criticism (BTW - are you doing the SQL for college course or work?) which may help you in future.
I think you're nesting too much which will have performance issues on large databases (I work with a data warehouse where this is an issue ). All the bracketing doesn't aid readability either especially if you have a high number of joins (imagine if you had to add another 7 or 8 tables!.

Try the below (as you can see, it's pretty easy to add or remove joins without worrying about where your brackets are or how nested it is)

SELECT dog.kcname, dam.kcname, sire.kcname, dog.petname, breed.breed, sex.sex
FROM Dog dog
INNER JOIN Dog dam ON d.DogID = dog.Dam
INNER JOIN Dog sire ON s.DogID = dog.sire
INNER JOIN Breed breed ON b.BreedID = dog.BreedID
INNER JOIN Sex sex ON g.SexID = dog.SexID
WHERE dog.DogID = 1;

HTH
Reply
Old Oct 8, 2003 | 10:52 AM
  #6  
ScoobyWon't's Avatar
ScoobyWon't
Scooby Regular
 
Joined: May 2002
Posts: 16,694
Likes: 0
From: Pot Belly HQ
Post

I'm attempting to teach myself ColdFusion/SQL in one go! Brackets are courtesy of Access generating the sql.

Have noticed my way is just returning the same name for pup, father and mother when queried through a web page through the server but when queried in Accessit provides the correct info. Bugger

I shall give it a go as you've suggested and see if it speeds up performance/works!.


[Edited by ScoobyWon't - 10/8/2003 10:57:24 AM]

[Edited by ScoobyWon't - 10/8/2003 11:00:28 AM]
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Phil3822
ICE
3
Sep 26, 2015 07:12 PM
jason62
General Technical
13
Sep 21, 2015 05:20 PM
stevem2k
Non Scooby Related
8
Jul 6, 2011 11:11 PM
dsmith
Computer & Technology Related
4
Feb 13, 2002 12:38 PM
Puff The Magic Wagon!
Computer & Technology Related
3
Feb 2, 2002 04:04 PM




All times are GMT +1. The time now is 10:44 AM.