473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with sql query

Hi there,

I need help extracting data from three seperate tables. The Tables look like
so:

Courses Table:
COURSE_ID | SUBJECT_TITLE | DESCRIPTION

Scheduled_Cours es Table:
SCHEDULE_ID | START_DATE | END_DATE | COURSE_ID | INSTRUCTOR_ID

Enroll Table:
SCHEDULE_ID | ENROLL_DATE | STUDENT_ID|

What I want to obtain are the ENROLL.SCHEDULE _ID and COURSES.SUBJECT _TITLE
fields when passing the student id to the ENROLL table. The fields named
with an "ID" suffix are just primary and foreign keys. The objective is to
find out which scheduled courses a particular student is already enrolled
in.

I attempted the following which just gives me an empty set:
mysql> SELECT ENROLL.SCHEDULE _ID,COURSES.SUB JECT_TITLE FROM
ENROLL,SCHEDULE D_COU
RSES,COURSES WHERE ENROLL.STUDENT_ ID=36 AND
SCHEDULED_COURS ES.COURSE_ID=CO URSES.COURSE_ID ;

What do I need to change to make this work?

I want to wind up with the following:
_______________ _______________ ___
SCHEDULE_ID | SUBJECT_TITLE
_______________ _______________ ___
32 | Biology 101
_______________ _______________ ___
46 | English 101
Thanks,

Alan Shiers
Jul 23 '05 #1
2 1295
Alan Shiers wrote:
I attempted the following which just gives me an empty set:
mysql> SELECT ENROLL.SCHEDULE _ID,COURSES.SUB JECT_TITLE FROM
ENROLL,SCHEDULE D_COU
RSES,COURSES WHERE ENROLL.STUDENT_ ID=36 AND
SCHEDULED_COURS ES.COURSE_ID=CO URSES.COURSE_ID ;


Well, I see you're missing a join condition between ENROLL and
SCHEDULED_COURS ES, but that should result in _more_ records, not fewer.

I'd also write the query differently, because I prefer to use row
aliases and SQL-92 join syntax:

SELECT E.SCHEDULE_ID, C.SUBJECT_TITLE
FROM ENROLL AS E
INNER JOIN SCHEDULED_COURS ES AS S ON E.SCHEDULE_ID = S.SCHEDULE_ID
INNER JOIN COURSES AS C ON C.COURSE_ID = S.COURSE_ID
WHERE E.STUDENT_ID = ?

All I can think of is that your values for course_id in the COURSES and
SCHEDULED_COURS ES tables don't match up. Try a couple of queries to
test if you have matches:

SELECT C.COURSE_ID, S.COURSE_ID
FROM COURSES AS C
LEFT OUTER JOIN SCHEDULED_COURS ES AS S ON C.COURSE_ID = S.COURSE_ID
WHERE S.COURSE_ID IS NULL

The above should display NULL for the S.COURSE_ID if you have no
sessions scheduled for a given course. This could occur either if you
mis-entered the data for the scheduled courses, or if a given course is
not being held.

SELECT C.COURSE_ID, S.COURSE_ID
FROM COURSES AS C
RIGHT OUTER JOIN SCHEDULED_COURS ES AS S ON C.COURSE_ID = S.COURSE_ID
WHERE C.COURSE_ID IS NULL

The above should display NULL for the C.COURSE_ID if you have no course
on record for a given scheduled course. I.e. the course_id of the
scheduled one does not exist. I assume this should _not_ happen, so it
would indicate a data integrity problem.

Now compare these two lists. Do you see any patterns of course_ids that
_should_ match up, but don't? For instance, perhaps someone entered
"XYZ123" for the course id in the courses table, but "XYZ-123" in the
scheduled_cours es table.

Regards,
Bill K.
Jul 23 '05 #2
Thanks Bill,
I'm afraid I'm new to SQL so I don't completely understand all this
business of INNER JOINS, LEFT OUTER JOINS, etc.
I'm totally lost when it comes to having to use these syntax. The statement
you provided certainly did work. I just don't understand why it works.
Having said that, maybe you can help me with another statement. The last
statement was used to display only those scheduled course records the
student was already enrolled in. Now, I need one that does just the
opposite. Display those scheduled courses that the student is not yet
enrolled in.

Following your lead with the aliases I came up with the followng (which
doesn't work), but it's a starting place:

SELECT S.SCHEDULE_ID, C.SUBJECT_TITLE FROM COURSES AS C, SCHEDULED_COURS ES
AS S INNER JOIN ENROLL AS E ON (S.SCHEDULE_ID= E.SCHEDULE_ID AND
S.COURSE_ID=C.C OURSE_ID) WHERE E.STUDENT_ID=? AND E.SCHEDULE_ID IS NULL;

Please advise,

Alan
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3056
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
9
3137
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
6
2429
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that will return the c date for TypeID 18 if it exist else the date for TypeID 1, for all jobs. the table structure is the following Job TypeID
3
1864
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype, answer) They are related by quesnum and questype. There are records in
7
2370
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
3
10666
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
0
2261
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
10
2594
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only tool you know how to use is a hammer, every problem tends to look like a nail. That said, I could solve my problem in C, but it's not the right tool. I need to come into the Windows world, and I need to get this done in Access or something...
7
2036
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First has about 30 fields. Every entry in this table will be unique. Second table has about 7 fields and is for reference - strictly a look up type table. I want to use one field, say FAMILY in the first table to look up any one of the 400 items in the...
3
2561
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks like the following: <a href="?searchtype=2">Community</a>
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.