Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Database Access / July 2008



Tip: Looking for answers? Try searching our database.

SQL query problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mattastic - 03 Jul 2008 16:43 GMT
I have table of courses and another table called courseSession, the course
session table contains the courseID of courses that occur more than once
throughout the year, to save repeating data, such as course title, description
etc.

I'd like to display occurances of the course, underneath the coursetitle.

I can do this using nested queries, but I'd like to use something a bit more
efficient.

Can anyone please help?

Thankyou

Course Table
----------
courseID - pk
courseCode
courseDescription
courseFee
courseWeeks
courseStartdate
courseEndtime
courseEconomic

Course session
-------------
sessionID -PK
sessionCourse - FK - contains the courseid from courses table for courses that
appear more than once
sessionStartDate
sessionEndDate
Ian Skinner - 03 Jul 2008 16:55 GMT
>  Can anyone please help?
>
>  Thankyou

The concept you are asking for help with is table joins in SQL.
Specifically for your case is probably an INNER JOIN.

I've often heard good recommendations for the book "Teach yourself SQL
in 10 minutes" by Ben Forta for a good beginning tomb on basic SQL concepts.

There are also plenty of SQL tutorials and blogs all over the internets.

The basic concept here is:

SELECT aTable.aField, bTable.bField
FROM aTable INNER JOIN bTable ON
     aTable.keyField = bTable.foreignKeyField
paross1 - 03 Jul 2008 17:02 GMT
Something like this maybe?

<cfquery name = "your_query" ... etc>
SELECT c.courseDescription,
s.sessionID,
s.sessionStartDate,
s.sessionEndDate
FROM course c
INNER JOIN session s ON c.courseID = s.sessionCourse
ORDER BY c.courseDescription
</cfquery>

<cfoutput query = "your_query" group = "courseDescription">
#courseDescription#
<cfoutput>
#sessionID# #sessionStartDate# #sessionEndDate#
</cfoutput>
</cfoutput>

Phil
Dan Bracuk - 03 Jul 2008 17:34 GMT
[q][i]Originally posted by: [b][b]Mattastic[/b][/b][/i]
I have table of courses and another table called courseSession, the course
session table contains the courseID of courses that occur more than once
throughout the year, to save repeating data, such as course title, description
etc.

I'd like to display occurances of the course, underneath the coursetitle.

I can do this using nested queries, but I'd like to use something a bit more
efficient.

Can anyone please help?

Thankyou

Course Table
----------
courseID - pk
courseCode
courseDescription
courseFee
courseWeeks
courseStartdate
courseEndtime
courseEconomic

Course session
-------------
sessionID -PK
sessionCourse - FK - contains the courseid from courses table for courses that
appear more than once
sessionStartDate
sessionEndDate

[/q]
Nothing to do with your actual question, but you might want to look at your
schema.  Things that jump out at me are the courseStartDate, courseEndTime, and
courseFee fields.  Those might be better off in the course session table.
Mattastic - 04 Jul 2008 10:18 GMT
Thanks for all the help - I dont do nearly as much database stuff as I'd like!
Mattastic - 11 Jul 2008 16:07 GMT
My query below, works great, apart from the fact I need to show courses that
don't appear in both tables. Is there a way to do this?

Thankyou

SELECT
c.courseID,
c.courseTitle,
s.sessionID,
s.sessionStartDate,
s.sessionEndDate
FROM ptcourses08 c
INNER JOIN coursesession s ON c.courseID = s.sessionCourse
ORDER BY c.courseTitle
Ian Skinner - 11 Jul 2008 16:22 GMT
> My query below, works great, apart from the fact I need to show courses that
> don't appear in both tables. Is there a way to do this?

When you want records from one table whether or not a record exists in
the second joined table you want an OUTER JOIN rather then an INNER JOIN.

One uses LEFT or RIGHT OUTER JOIN to say which table has the optional
data in it, the table to the left or the right of the join statement.

My guess would be that you want courses whether or not there is a course
session so something like this should be close.

SELECT ...
FROM ptcourses 08 c
LEFT OUTER JOIN coursesession s ON c.courseID = sessionCourse
...
Mattastic - 14 Jul 2008 10:16 GMT
Thanks thats great
indnajns - 15 Jul 2008 13:48 GMT
"One uses LEFT or RIGHT OUTER JOIN to say which table has the optional data in
it, the table to the left or the right of the join statement."

FINALLY!  An explanation I can understand!  I have never understood the Right
vs Left concept until now.  Could you please explain Inner Join with the same
clarity?
Ian Skinner - 15 Jul 2008 14:31 GMT
> "One uses LEFT or RIGHT OUTER JOIN to say which table has the optional data in
> it, the table to the left or the right of the join statement."
>
>  FINALLY!  An explanation I can understand!  I have never understood the Right
> vs Left concept until now.  Could you please explain Inner Join with the same
> clarity?

Can't say why it's called 'inner' must be some kind of mathematical
thing.  But an inner join is a join between two tables where both tables
must have a matching record, or no record is returned.  So if you join
table A to table B on field A equals field B then there must be a record
in both table A and B with a matching value in field A and field B or
nothing is returned.

A lot of databases make the key word 'inner' optional and it can be
shortened to just JOIN.

IE. tableA INNER JOIN tableB ... is the same as tableA JOIN tableB ...

The former is considered more explicit and clear by some.
Ian Skinner - 15 Jul 2008 14:33 GMT
> "One uses LEFT or RIGHT OUTER JOIN to say which table has the optional data in
> it, the table to the left or the right of the join statement."
>
>  FINALLY!  An explanation I can understand!  I have never understood the Right
> vs Left concept until now.  Could you please explain Inner Join with the same
> clarity?

Thanks, but I should point out that I wrote this backwards.  The table
you specify with the LEFT or the RIGHT is the table that will always
return records whether or not there are matching records in the other
table.  I.E. the opposite table is the optional table.
Mattastic - 15 Jul 2008 14:49 GMT
[q][i]Originally posted by: [b][b]indnajns[/b][/b][/i]
"One uses LEFT or RIGHT OUTER JOIN to say which table has the optional data in
it, the table to the left or the right of the join statement."

FINALLY!  An explanation I can understand!  I have never understood the Right
vs Left concept until now.  Could you please explain Inner Join with the same
clarity?[/q]

Thats exactly what I thought! I've never understood explanations of this up
until now.
paross1 - 15 Jul 2008 14:49 GMT
http://en.wikipedia.org/wiki/Join_(SQL)

[i]An inner join requires each record in the two joined tables to have a
matching record. An inner join essentially combines the records from two tables
(A and B) based on a given join-predicate. The result of the join can be
defined as the outcome of first taking the Cartesian product (or cross-join) of
all records in the tables (combining every record in table A with every record
in table B) - then return all records which satisfy the join predicate.[/i]

[i]The result of a left outer join (or simply left join) for tables A and B
always contains all records of the "left" table (A), even if the join-condition
does not find any matching record in the "right" table (B). This means that if
the ON clause matches 0 (zero) records in B, the join will still return a row
in the result?but with NULL in each column from B. This means that a left outer
join returns all the values from the left table, plus matched values from the
right table (or NULL in case of no matching join predicate).[/i]

Google is your friend.......

Phil
indnajns - 15 Jul 2008 15:40 GMT
Yes, but Ian said it in under 30 words.  30 words that made SENSE.  :)

And yes, after understanding your answer, I went out and started reading up on
Inner/Outer Joins and realized either I was reading it backwards or you had
mistyped.  Which is fine.  I GET IT NOW!  The next question is, is there a real
good reason to use Join over Where A=B?  Or is it just programmer preference?  
In particular, if one is joining three tables with several Joins/Where
conditions?
Ian Skinner - 15 Jul 2008 15:56 GMT
> Yes, but Ian said it in under 30 words.  30 words that made SENSE.  :)
>
[quoted text clipped - 4 lines]
> In particular, if one is joining three tables with several Joins/Where
> conditions?  

Yes as I replied this morning, my original post was mistyped and I had
reversed the relationship.

Your question of using a Join versus an Where A=B clause strongly speaks
to you being a user of Oracle databases.

The older Oracle syntax for joins used the where clause such as this:

SELECT ...
FROM aTable, bTable
WHERE aTable.aField = bTable.bField

One would use the astric[*] to desiginat outer joins.

SELECT ...
FROM aTable, bTable
WHERE aTable.aField* = bTable.bField
--LEFT OUTER JOIN--

SELECT ...
FROM aTable,bTable
WHERE aTable.aField = bTable.bField*
--RIGHT OUTER JOIN--

This is Oracle specific syntax and does not work in all databases.  The
SQL92 standard is to use the JOIN clause.  Newer Oracle databases now
support the standard.

So the main advantage to the JOIN syntax is that it is more universal.
I also find it easier and clearer when one starts having to join three
or more tables, because the JOIN ON makes it clearer what order to join
the tables together.  Especially when one uses parenthesis to specify
join order.

FROM
   aTable INNER JOIN
      (bTable INNER JOIN
         cTable ON bTable.key2 = cTable.key2)
      ON aTable.key1 = bTable.key1
Azadi - 15 Jul 2008 16:33 GMT
when to use JOIN and when to use WHERE clause to link your tables can
also depend on how your db utilizes table indexes.
check your db reference manual on indexing and index optimization - it
may cover the topic of when to use JOIN and when to use WHERE clause to
join your tables (mysql ref manual does).

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Dan Bracuk - 15 Jul 2008 17:34 GMT
[q][i]Originally posted by: [b][b]indnajns[/b][/b][/i]
Yes, but Ian said it in under 30 words.  30 words that made SENSE.  :)

And yes, after understanding your answer, I went out and started reading up on
Inner/Outer Joins and realized either I was reading it backwards or you had
mistyped.  Which is fine.  I GET IT NOW!  The next question is, is there a real
good reason to use Join over Where A=B?  Or is it just programmer preference?  
In particular, if one is joining three tables with several Joins/Where
conditions?[/q]
Some db's support this syntax, which saves a lot of typing.

from table1 join table2 using (field1, field2, etc)

Also if are updating one table from another, using join syntax is a lot more
efficient.
paross1 - 17 Jul 2008 21:12 GMT
[Q]Yes, but Ian said it in under 30 words. 30 words that made
SENSE.[/Q]Interesting. If what I posted didn't make sense to you, then the
distinct possibility exists that you are in the wrong business.

Phil
Mattastic - 30 Jul 2008 16:00 GMT
I have another problem with my query.

Currently I'm using this sql to extract courses with multiple sessions:

SELECT * FROM ptcourses08 c , coursesession cs
WHERE 0=0
AND c.courseid = cs.sessioncourse
ORDER BY c.courseTitle

This works fine for courses that have sessions, but it doesnt pick up full
time courses that dont have sessions, courses that just appear in the
ptcourses08 table only.

Can anyone tell me how I can get all the courses?

Thanks in advance
Azadi - 30 Jul 2008 16:13 GMT
use LEFT/RIGHT [OUTER] JOIN:

SELECT *
FROM ptcourses08 c LEFT JOIN coursesession cs ON c.courseid =
cs.sessioncourse
ORDER BY c.courseTitle

and don't use SELECT * - list only columns you actually need returned by
your query instead.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
paross1 - 30 Jul 2008 16:21 GMT
Wow, I guess that you missed the entire point of the previous answers to your
original question.....
You would need to do an OUTER (LEFT) join. Also, you probably should list the
fields that you want to select individually, rather than SELECT *. That way,
you can perform alternative actions on those fields that may be NULL because
they are in the joined table that has no matching rows.

SELECT *
FROM ptcourses08 c
LEFT JOIN coursesession cs ON c.courseid = cs.sessioncourse
ORDER BY c.courseTitle

Phil
Mattastic - 30 Jul 2008 16:30 GMT
Thanks Phil.

Do you recommend I change my Db design?
paross1 - 30 Jul 2008 16:37 GMT
Change your query to select all courses, and any sessions that happen to be associated (related) to them..... by doing an OUTER JOIN.... which was the whole point of this entire thread.

Phil
Mattastic - 30 Jul 2008 16:53 GMT
OK thanks

My original question was for a different page, I didnt realise I could use it with my search too.
Dan Bracuk - 30 Jul 2008 16:21 GMT
[q][i]Originally posted by: [b][b]Mattastic[/b][/b][/i]
I have another problem with my query.

Currently I'm using this sql to extract courses with multiple sessions:

SELECT * FROM ptcourses08 c , coursesession cs
WHERE 0=0
AND c.courseid = cs.sessioncourse
ORDER BY c.courseTitle

This works fine for courses that have sessions, but it doesnt pick up full
time courses that dont have sessions, courses that just appear in the
ptcourses08 table only.

Can anyone tell me how I can get all the courses?

Thanks in advance[/q]
As long as you keep your current database design, you are going to have these
problems.  A better way to do it is for the course session table to store every
occurence of a course, not just the ones that occur more than once a year.
Mattastic - 30 Jul 2008 16:27 GMT
Thanks Dan.

Then my query would work ok I take it?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.