473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to SELECT data using multiple tables

I am new to MySql and PHP and am hoping that somone can help me with a query.

I have a table called Test
within it are several fields including:
course_ID
Test ID

so within this table data a Course (course_ID) can have several test_IDs associated with it.
So lets say course_ID (3) has test_IDs (2,4,5,7,9)
and course_ID (5) has test_IDs (1,15,22,44,33,55) associated with it.

I have another table called Test_results
within it are several fields including:
Member_ID
Test_ID
Final_Score

What I would like to do is query the Test_results table for a specific member (member_ID) but only bring back the data associate with a specific course (or the test_IDs for that course)

If I use :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM "Test_results" WHERE Member_ID=12 ORDER BY Test_ID
  2.  
I get all the test taken for member 12 in all courses he has taken tests in.

Is there some way to request just the results of all the tests in just one course ?

I hope I've made sense ,,,

Any help would be greatly appreciated.

Brad
Jul 3 '07 #1
6 12269
r035198x
13,262 8TB
I am new to MySql and PHP and am hoping that somone can help me with a query.

I have a table called Test
within it are several fields including:
course_ID
Test ID

so within this table data a Course (course_ID) can have several test_IDs associated with it.
So lets say course_ID (3) has test_IDs (2,4,5,7,9)
and course_ID (5) has test_IDs (1,15,22,44,33,55) associated with it.

I have another table called Test_results
within it are several fields including:
Member_ID
Test_ID
Final_Score

What I would like to do is query the Test_results table for a specific member (member_ID) but only bring back the data associate with a specific course (or the test_IDs for that course)

If I use :
SELECT * FROM "Test_results" WHERE Member_ID=12 ORDER BY Test_ID

I get all the test taken for member 12 in all courses he has taken tests in.

Is there some way to request just the results of all the tests in just one course ?

I hope I've made sense ,,,

Any help would be greatly appreciated.

Brad
1.) "Specified course" means you are supplying a course_ID?
Then you want to do
Expand|Select|Wrap|Line Numbers
  1. select * from Test_results where (Member_ID = 12) and (Test_ID in (select Test_ID from Test where course_ID = "givenCourseID"));
N.B You should probably aliase your tables appropriately.
Jul 3 '07 #2
Thank you so much for the quick response and I really appreciate you taking the time to help me as I struggle through this.

This is probably a real stupid question but what do you mean by aliase as it pertains to the tables ?

When I inserted the query you sent it errored out.

My internal variables are
Member_ID = $vlcmbid
Course_ID = $vlcsid

so the query looks like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Test_results WHERE (Member_ID = $vlcmbid) AND (Test_ID IN (SELECT Test_ID FROM Test WHERE course_ID = $vlcsid));
  2.  
Actually the PHP code looks like:
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM ".TABLE_PREFIX."Test_results WHERE (Member_ID = $vlcmbid) AND (Test_ID IN (SELECT Test_ID FROM Test WHERE course_ID = $vlcsid))";
  2. $result = mysql_query($query);
  3.  
When I execute the PHP script I doesn't return anything which causes it to error out after that when it tries to evaluate the results.

Thanks again as I am totally lost on how to resolve this.

Brad
Jul 3 '07 #3
Or You Could Use This :

Expand|Select|Wrap|Line Numbers
  1. SELECT TR.* FROM Test T, Test_results TR
  2. WHERE T.Test_ID = TR.Test_ID
  3. AND       T.Course_ID = GivenCourseID
  4. AND       TR.Member_ID = GivenMemberID
  5.  
I Haven't Checked It, But I Think It Will Work.
Jul 3 '07 #4
r035198x
13,262 8TB
Thank you so much for the quick response and I really appreciate you taking the time to help me as I struggle through this.

This is probably a real stupid question but what do you mean by aliase as it pertains to the tables ?

When I inserted the query you sent it errored out.

My internal variables are
Member_ID = $vlcmbid
Course_ID = $vlcsid

so the query looks like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Test_results WHERE (Member_ID = $vlcmbid) AND (Test_ID IN (SELECT Test_ID FROM Test WHERE course_ID = $vlcsid));
  2.  
Actually the PHP code looks like:
Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM ".TABLE_PREFIX."Test_results WHERE (Member_ID = $vlcmbid) AND (Test_ID IN (SELECT Test_ID FROM Test WHERE course_ID = $vlcsid))";
  2. $result = mysql_query($query);
  3.  
When I execute the PHP script I doesn't return anything which causes it to error out after that when it tries to evaluate the results.

Thanks again as I am totally lost on how to resolve this.

Brad
See the sql posted above. The variables T and TR e.t.c are the aliases that I was talking about.
Jul 4 '07 #5
Atli
5,058 Expert 4TB
Or You Could Use This :

Expand|Select|Wrap|Line Numbers
  1. SELECT TR.* FROM Test T, Test_results TR
  2. WHERE T.Test_ID = TR.Test_ID
  3. AND       T.Course_ID = GivenCourseID
  4. AND       TR.Member_ID = GivenMemberID
  5.  
I Haven't Checked It, But I Think It Will Work.
This query could also be written like this. It may explain better (or not, what do I know!)
Expand|Select|Wrap|Line Numbers
  1. SELECT tr.*
  2. FROM Test AS t
  3. INNER JOIN Test_results AS tr
  4.   ON tr.Test_ID = t.Test_ID
  5. WHERE
  6.   t.Course_ID = GivenCourseID
  7. AND 
  8.   tr.Member_ID = GivenMemberID
  9.  
Jul 4 '07 #6
Atli
5,058 Expert 4TB
I have edited the thread's title to better describe it's contents.
Please read the Posting Guidlines before posting

Moderator
Jul 4 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Beyonder | last post by:
I have five tables in my database, there are actually NO common fields between them, not even a KEY or ID or anything like that, except for the "body" of a blob field. and that text is not...
9
by: Rowland Hills | last post by:
I have a table which is returning inconsistent results when I query it! In query analyzer: If I do "SELECT * FROM TABLE_NAME" I get no rows returned. If I do "SELECT COL1, COL2 FROM...
1
by: avinash | last post by:
hi myself avi i am developing one appliacaion in which i am using vb 6 as front end, adodb as database library and sql sever 7 as backend. i want to update one table for which i required data from...
6
by: Eric Robinson | last post by:
Hi all, I'm having real trouble wrapping my newbie brain around this problem. Can someone please tell me the most efficient (or any!) way to write a SELECT statement to return a set of rows from...
1
by: Ahmet Karaca | last post by:
Hi. myds.Reset(); mycommand.SelectCommand.CommandText= "Select att1 from Ing as Ingredient, Pro as Product "+ "where Pro.ad='apple' and Pro.id=Ing.id"; mycommand.Fill(myds, "Product"); // Here...
19
by: Shwetabh | last post by:
Hi, I have two tables: Code and Color. The create command for them is : create table Color( Partnum varchar(10), Eng_Color char(10), Span_Color char(20), Frch_Color char(20), CONSTRAINT...
2
by: chopin | last post by:
I am using Microsoft Access, and VBA. I was wondering if it was possible to select multiple tables using DAO. For example, here is the code I am thinking should work, but doesn't: sSQL =...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add...
3
by: sangam56 | last post by:
Hello!I am using following sql statement: SELECT Menu.MenuID,Menu.TextUrl FROM Menu WHERE Menu.MenuID= (SELECT Permissions.MenuID FROM Permissions WHERE Permissions.RoleID=(SELECT Roles.RoleID...
12
by: micarl | last post by:
How would i print a report based on criteria selected from several Combo Boxes as well as multiple Multi Select List Boxes, that are located on the same form? I can get one Multi List Box, just...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.