Jump to content


Queries between 2 tables..

  • Please log in to reply
2 replies to this topic

#1 blackdevil

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 31 March 2006 - 01:43 PM

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..


#2 jvrothjr

  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 31 March 2006 - 04:46 PM

$query = ("SELECT Table1.Student Metric FROM Table1 INNER JOIN Table2 ON Table1.Student Metric = Table2.Student Metric");

If you understand everything you know nothing!


#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 March 2006 - 07:59 PM

SELECT t1.`Student Metric`
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.`Student Metric` = t2.`Student Metric`
WHERE t2.`Student Metric` IS NULL

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users