d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Knows About Sql Queries?
Add Reply New Topic New Poll
Member
Posts: 28,641
Joined: Jan 23 2007
Gold: 755.00
Mar 27 2013 07:01pm
I was wondering if anyone had some knowledge on how to write SQL queries
I am quite clueless and I'm trying to write a simple query where I need to obtain the data from 3 different tables.

Anyone is willing to give me a hand understanding how to do it ?

Let's say I have table Client with CLIENT_ID as a primary key
Table Task with TASK_CODE as primary key and foreign key CLIENT_ID is there as well
Table Package with PACKAGE_ID as primary key

I was wondering how I could go and retrieve let's say a particular CLIENT_ID for a specific PACKAGE_ID let's say using a SQL query (in this case it would only involve 2 tables but let's say I need the TASK_CODE for some reason)

Cheers and any help would be appreciated
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Mar 27 2013 07:32pm
What is the reference that the Package table has to Task or Client? With some assumptions made about your table structure:

Code

SELECT
   c.CLIENT_ID,
   t.TASK_CODE
FROM
   Client c,
   Table t,
   Package p
WHERE
   c.CLIENT_ID = t.CLIENT_ID
   AND t.TASK_CODE = p.TASK_CODE
   AND p.PACKAGE_ID = ?


Since you didn't give me a complete set of relations I'm guessing there is something in Table that connects a particular Client with a Package.
Member
Posts: 28,641
Joined: Jan 23 2007
Gold: 755.00
Mar 27 2013 07:44pm


Those would be my messy relationships
Since Visio doesn't allow M-to-M relationships it complicates things especially that I've never really done any database work
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 27 2013 07:46pm
Quote (WDZepplin @ Mar 27 2013 09:44pm)
http://img526.imageshack.us/img526/8139/sqlinquery.png

Those would be my messy relationships
Since Visio doesn't allow M-to-M relationships it complicates things especially that I've never really done any database work


have you even tried his query? it looks like it'll work
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Mar 27 2013 07:56pm
Code
SELECT
   i.IND_NAME
FROM
   Individual i,
   Assignment a,
   Task t,
   Package p
WHERE
  i.IND_NAME = a.IND_NAME
  AND a.ASSIGNMENT_ID = t.ASSIGNMENT_ID
  AND t.TASK_CODE = p.TASK_CODE
  AND p.PACKAGE_ID = ?


That's of course assuming you need something from Task as well as just getting the IND_NAME from Individual. If you just needed the name(s), you could just join Individual to Package on the IND_NAME. Also, you probably shouldn't ever use a VARCHAR2 containing people's names as a primary key. Really bad form.

This post was edited by rockonkenshin on Mar 27 2013 07:58pm
Member
Posts: 28,641
Joined: Jan 23 2007
Gold: 755.00
Mar 27 2013 08:59pm
Quote (rockonkenshin @ Mar 27 2013 09:56pm)
Code
SELECT
   i.IND_NAME
FROM
   Individual i,
   Assignment a,
   Task t,
   Package p
WHERE
  i.IND_NAME = a.IND_NAME
  AND a.ASSIGNMENT_ID = t.ASSIGNMENT_ID
  AND t.TASK_CODE = p.TASK_CODE
  AND p.PACKAGE_ID = ?


That's of course assuming you need something from Task as well as just getting the IND_NAME from Individual. If you just needed the name(s), you could just join Individual to Package on the IND_NAME. Also, you probably shouldn't ever use a VARCHAR2 containing people's names as a primary key. Really bad form.


yeah should have done it with a IND_ID instead of the name, would have been much better I realize it now
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll