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
https://forums.phpfreaks.com/topic/138685-query-over-two-tables/
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.

Archived

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

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