Jump to content

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

Edited by Barand

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.

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

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.