Jump to content

SELECT Help


rseigel
Go to solution Solved by rseigel,

Recommended Posts

Hi all,

 

I'm sure this should be simple but for some reason my brain won't cooperate.

 

I have 2 tables:

 

ps_products and tmp_BF

 

Both of them have a field called supplier_reference.

 

I need to know which records are in ps_products that aren't in tmp_BF and vice-versa.

 

Could someone please kick me in the right direction?

 

Thanks,

 

Ron

Link to comment
Share on other sites

As two separate queries? Start with ps_products, LEFT JOIN (so it's optional) the tmp_BF table on that field, then filter the results to WHERE tmp_BF's supplier_reference IS NULL. A matching row "can't" possibly have that value as NULL so it'll only return the results without a match. Reverse the process for the other direction.

 

Or are the tables the same and you'd like one query to show which rows from which tables are missing?

Edited by requinix
Link to comment
Share on other sites

Thanks very much for the help....

 

I actually have the SELECT working:

$result = mysqli_query('SELECT * FROM tmp_BF WHERE tmp_BF.supplier_reference NOT IN (SELECT supplier_reference FROM ps_product)')
    or die(mysql_error());
echo "NEW PRODUCTS<br /><br />";
while($row = mysqli_fetch_array($result))
  {
   echo "<a href='https://www.mysupplier.com/item/item.lasso?dsc2={$row['supplier_reference']}'>{$row['supplier_reference']}</a><br />";
  }

The problem now is that it outputs nothing.

 

If I do the raw SELECT on the database there is a result.

 

Now I"m really confused....

Link to comment
Share on other sites

Fixed #1.

 

As far as #2, I'm not sure what you mean.

 

I have the connection at the top of my script.

$con = mysqli_connect("localhost","xxxxxx","xxxxx","xxxxx");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
echo "Connected to Database<br /><br />";
Link to comment
Share on other sites

  • Solution

I'm a moron.... :)

 

This works perfectly:

 

$result = mysqli_query($con,'SELECT * FROM tmp_BF WHERE tmp_BF.supplier_reference NOT IN (SELECT supplier_reference FROM ps_product)')
    or die(mysqli_error());
 
Thanks for the kick in the right direction.
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.