Solved

Select Statement from mutliple tables

Posted on 2013-11-19
13
320 Views
Last Modified: 2013-11-19
Hi,
I am trying select all the class names from tbl_class_Names that are not listed in
tbl_students.

I have tried this select statement however it does not work..

select tbl_Class_Names.Class_Name 
from tbl_Class_Names
join tbl_Class on tbl_Class.Class_ID = tbl_Students.Class_ID
join tbl_Class_Names on tbl_Class_Names.CN_ID = tbl_Class.CN_ID
where tbl_Students.Class_ID <> tbl_Class on tbl_Class.Class_ID

Open in new window


I have inherited the database so the structure is not ideal.

tbl_Students
Student_Number            Class_ID                  Lecture_Room
511                              18                             5
512                              15                             9


tbl_Class

Class_ID            CN_ID
18            55            
15            76


tbl_Class_Names
CN_ID            Class_Name
55               ScienceY2      
76               MathY3
0
Comment
Question by:jay_eire
  • 6
  • 5
  • 2
13 Comments
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
This should work:

select tbl_Class.Class_ID, tbl_Class.CN_ID, tbl_Class_Names.Class_Name
from tbl_Class
JOIN tbl_Class_Names on tbl_Class.CN_ID = tbl_Class_Names.CN_ID
where tblStudents.Class_ID not in (select tbl_Class.Class_ID from tbl_Class)

Open in new window

0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
select cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.class_id
WHERE c.class_id not in (select class_id from tbl_students)

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
Comment Utility
thanks for reply

@sjwales
I am getting this error message back
Error Code: 1054. Unknown column 'tbl_Students.Class_ID' in 'IN/ALL/ANY subquery'      0.000 sec
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
Comment Utility
corrected join condition :

select cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.cn_id
WHERE c.class_id not in (select class_id from tbl_students)

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
Comment Utility
@ jacko72 thanks what is c and cn? are these temporary values?
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
They are aliases, you use them as a shortcut for the table names
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:jay_eire
Comment Utility
thanks jack072 I have an error

Error Code: 1052. Column 'cn_id' in field list is ambiguous      0.000 sec
0
 
LVL 7

Author Comment

by:jay_eire
Comment Utility
my bad, I have corrected it had  WHERE c.cn_id instead of WHERE c.class_id
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
ensure you have prefixed all the column names with an alias or table name.
0
 
LVL 7

Author Comment

by:jay_eire
Comment Utility
if I need to select additional columns from tbl_Class_Names how do I do this with the alias?
0
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
My query was incorrect, reversed the conditions, Jacko's looks more like the trick.

Tbl_Class_Names is aliased above as "cn".

You can add extra columns by cn.col1, cn.col2.  Just because a table is aliased, by the way, doesn't mean you still can't reference it by its full name.

So you could do cn.col1 or you could do tbl_Class_names.col1
0
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
The alias for tbl_class_names is cn, you can see the alias after the tablename in the FROM clause : from tbl_class_names cn
You just need to prefix any colum name with the alias then a full stop and then the column name like : cn.class_id

so:

select cn.class_id, cn.class_name
from tbl_class_names cn
   INNER JOIN class c ON cn.cn_id = c.cn_id
WHERE c.class_id not in (select class_id from tbl_students) 

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
Comment Utility
thank you kindly for the help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now