I think the problem is in your table structure. If you did this:

Students:
- Name
- StudentID

Classes:
- ClassID
- Name

StudentsClasses:
- StudentID (PK)
- ClassID (PK)

(You make them a combined key by doing PRIMARY KEY (StudentID,ClassID) in
your table def.)

When you add a student to a class you add them to "StudentClasses" table. In
your previous layout (which was not normalized completely) you are repeating
Name/StudentID each time a student is put into a class (redundant data is a
no-no).

Now to get all classes you just:

SELECT C.* FROM Classes AS C, StudentsClasses AS R WHERE
R.StudentID='364326' AND R.ClassID=C.ClassID

--Joe

--
Joe Stump <[EMAIL PROTECTED]>
http://www.joestump.net


-----Original Message-----
From: Lefevre, Steven [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 08, 2003 8:22 AM
To: [EMAIL PROTECTED]
Subject: subselect workaround help?


Hey folks-

'nother question.
I'm not an SQL expert, and I think I need a subselect, which means I need a
workaround on MySQL 3.23 or whatever version it is.

Here's the tables I have, with the relevant columns:

Students
 - Name
 - StudentID (PK)
 - ClassID

Classes
 - ClassID (PK)
 - Name

 Each Student belongs to one class.


So, I have the StudentID. I want to get the Classes.Name...
Final result should look like

Student | Class
--------------------------
Steve Lefevre | Math101
Stacy Adams | Intro to SQL

Something like

SELECT Student.Name, Classes.Name FROM Students, Classes WHERE
Students.StudentID = " . $ID . " AND Classes.Name IN ( SELECT Classes.Name
FROM Classes WHERE ClassID = Students.ClassID )

I know that probably won't work as a subselect query, but that's my starting
point.

Any help? Thanks!

Steve


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail
<[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to