Jump to content

Query Over Two Tables


mrhenniger

Recommended Posts

First time poster here.  I have dabbled with MySQL via PHP.

 

I have done queries across multiple tables using JOINs.

 

Here is the problem I am currently considering.  Table1...

Serial - int

OtherColumnA

OtherColumnB

...the values for Serial in this table are unique.

 

Table2...

Serial - int

StuffA

StuffB

...there can be duplicate values for Serial (non-unique).

 

I would like to create a MySQL query that will list all the values of Serial in Table1 which DON'T EXIST in Serial in Table2.

 

I don't know enough about MySQL to solve the problem.

 

Can anyone here help?  Thanks in advance for any assistance you can provide.

 

Regards,

 

Mike

Link to comment
Share on other sites

This is the postgresql forum (and your topic will be moved over to the mysql forum shortly), but the answer is the same.

 

SELECT * FROM t1 LEFT JOIN t2 USING (serial)

 

This query will combine rows from t1 and t2, matching them up by the serial value.  If there's no match, then t1.serial will be set but t2.serial will be null.  So if you want all serials from t1 that aren't in t2 you just need to do this:

 

SELECT * FROM t1 LEFT JOIN t2 USING (serial) WHERE t2.serial IS NULL

 

Or "SELECT t1.serial" if you just need the serials.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.