Jump to content

Compare Mysql Table And Display Which Not Exist


shahzad429

Recommended Posts

I have two table

 

1. TableID, ProductName

 

2. TableID, AgencyID, ProductID

 

 

what i want to do is to display all the products from table 1 and exclude when TableID of table 1 match ProductID of table 2.

 

 

any help regarding this pelase

 

 

Thanks,

Shahzad

I'd do it like this

 

SELECT p.TableID, p.ProductName
FROM product AS p
LEFT JOIN agencytoproduct AS a ON p.TableID = a.productID
 AND a.AgencyID='$object[TableID]'
WHERE a.productID IS NULL
ORDER BY p.ProductName

  On 12/24/2012 at 9:45 AM, Barand said:

I'd do it like this

 

SELECT p.TableID, p.ProductName
FROM product AS p
LEFT JOIN agencytoproduct AS a ON p.TableID = a.productID
AND a.AgencyID='$object[TableID]'
WHERE a.productID IS NULL
ORDER BY p.ProductName

 

a.productId isn't null - it doesn't exist - which is why this method doesn't work.

  On 12/24/2012 at 10:17 AM, SofWare said:

a.productId isn't null - it doesn't exist - which is why this method doesn't work.

 

SofWare, don't knock it till you've tried it

 

mysql> SELECT * FROM product;
+------------+-------------+
| product_id | productName |
+------------+-------------+
|		  1 | Lawnmower   |
|		  2 | Greenhouse  |
|		  3 | Paving slab |
|		  4 | BBQ		 |
|		  5 | Chainsaw    |
+------------+-------------+


mysql> SELECT * FROM agencytoproduct;
+----+----------+-----------+
| id | agencyID | productID |
+----+----------+-----------+
|  1 |	    1 |		 1 |
|  2 |	    1 |		 2 |
|  3 |	    1 |		 5 |
|  4 |	    2 |		 1 |
|  5 |	    2 |		 3 |
|  6 |	    2 |		 4 |
+----+----------+-----------+

 

Note that for agency 1 there are no products 3 and 4.

 

Running my query:

 

mysql> SELECT p.product_id, p.ProductName
   -> FROM product AS p
   -> LEFT JOIN agencytoproduct AS a ON p.product_ID = a.productID
   -> AND a.AgencyID = '1'
   -> WHERE a.productID IS NULL
   -> ORDER BY p.ProductName;
+------------+-------------+
| product_id | ProductName |
+------------+-------------+
|		  4 | BBQ		 |
|		  3 | Paving slab |
+------------+-------------+

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.