Jump to content

Archived

This topic is now archived and is closed to further replies.

blackdevil

Queries between 2 tables..

Recommended Posts

HI all.

I am currently designing MysQL database and facing few problem about queries.
Here it goes. I have 2 table: table 1 (consists of 2 fields:Student Metric and Course Id) and table 2 (consists of 4 fields: Student Metric, Class Id, Date, Time). Now I need to list a query between the 2 tables that lists the Student metric fields which contained in table 1 but not contained in the table 2. How can I do that in PHP code communicating with MySQL database?

Thanks for any help in advance..

Cheers

Share this post


Link to post
Share on other sites
[code]
$query = ("SELECT Table1.Student Metric FROM Table1 INNER JOIN Table2 ON Table1.Student Metric = Table2.Student Metric");

[/code]

Share this post


Link to post
Share on other sites
[code]SELECT t1.`Student Metric`
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.`Student Metric` = t2.`Student Metric`
WHERE t2.`Student Metric` IS NULL[/code]

You need LEFT JOIN to find unmatched records. NULL values are returned from the right table (in this case Table2) if there is no match.

PS Avoid column and table names containing spaces

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.