selecting things that don't exist in another table

This is a discussion on selecting things that don't exist in another table within the MySQL Database forums, part of the Database Forums category; Say I had two tables and that each one had, within it, a common primary key (stock_num). eg. products_in_stock: stock_num ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-13-2007
yawnmoth
 
Posts: n/a
Default selecting things that don't exist in another table

Say I had two tables and that each one had, within it, a common
primary key (stock_num).

eg.

products_in_stock:
stock_num
quantity

products_ordered_for_stock:
stock_num
quantity

I'd like to be able to select all the stock_num's in products_in_stock
that aren't in products_ordered_for_stock. Any idea as to how I might
do this?

I realize the database could redone more efficiently (maybe just
making one table with three rows - stock_num, quantity_in_stock,
quantity_ordered), but unfortunately, that's not an option.

Any ideas?

Reply With Quote
  #2 (permalink)  
Old 04-13-2007
cal_in_az
 
Posts: n/a
Default Re: selecting things that don't exist in another table

On Apr 13, 9:12 am, "yawnmoth" <terra1...@yahoo.com> wrote:
> Say I had two tables and that each one had, within it, a common
> primary key (stock_num).
>
> eg.
>
> products_in_stock:
> stock_num
> quantity
>
> products_ordered_for_stock:
> stock_num
> quantity
>
> I'd like to be able to select all the stock_num's in products_in_stock
> that aren't in products_ordered_for_stock. Any idea as to how I might
> do this?
>
> I realize the database could redone more efficiently (maybe just
> making one table with three rows - stock_num, quantity_in_stock,
> quantity_ordered), but unfortunately, that's not an option.
>
> Any ideas?


Try this:

select pi.stock_num
from products_in_stock pi
left join products_ordered_for_stock po
on pi.stock_num = po.stock_num
where po.stock_num is null;



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0