1

I have a select list that shows a list that shows this:

Tech Name | Tech Zip | Tech States  (Value=Tech ID)

All of this is coming from a recordset. Now, I want to see whether or not this tech exists in another table. If he does NOT, then I want to show him in the select list. Otherwise, ignore him.

UPDATE

Using the stuff from the answer below, this is what I've come up with. I think it is working. Is this the best way to do this?

mysql_select_db($database_localhost, $localhost);
 $query_NeededTechs = "
    SELECT  l.*
    FROM    zip_tech l
    WHERE   NOT EXISTS
    (
    SELECT  tech_name
    FROM    zip_zip r
    WHERE   r.tech_name = l.tech_name
    )
 ";
$NeededTechs = mysql_query($query_NeededTechs, $localhost) or die(mysql_error());
$row_NeededTechs = mysql_fetch_assoc($NeededTechs);
$totalRows_NeededTechs = mysql_num_rows($NeededTechs);

My Select List:

<select name="select" id="select">
      <?php
do {  
?>
      <option value="<?php 
      echo $row_NeededTechs['tech_id']?>"><?php echo $row_NeededTechs['tech_name'] . " | " . $row_NeededTechs['tech_zip'] . " | " . $row_NeededTechs['states']?></option>
      <?php
} while ($row_NeededTechs = mysql_fetch_assoc($NeededTechs));
  $rows = mysql_num_rows($NeededTechs);
  if($rows > 0) {
      mysql_data_seek($NeededTechs, 0);
      $row_NeededTechs = mysql_fetch_assoc($NeededTechs);
  }
?>
    </select>
SherwoodPro
  • 105
  • 10

1 Answers1

1

I'm not entirely sure I understand your question, but it seems as though you want to select all 'techs' from one table that do not exist in another?

If so, this is the perfect read for you [with examples]: NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL

Which method is best to select values present in one table but missing in another one?

this:

SELECT  l.*
FROM    t_left l
LEFT JOIN
        t_right r
ON      r.value = l.value
WHERE   r.value IS NULL

or this:

SELECT  l.*
FROM    t_left l
WHERE   l.value NOT IN
        (
        SELECT  value
        FROM    t_right r
        )

or this:

SELECT  l.*
FROM    t_left l
WHERE   NOT EXISTS
        (
        SELECT  NULL
        FROM    t_right r
        WHERE   r.value = l.value
        )

Sources:

Related StackOverflow Question

ExplainExtended

Community
  • 1
  • 1
Stephen Lake
  • 1,582
  • 2
  • 18
  • 27
  • 1
    SO reading about this, it seems like LEFT JOIN or RIGHT JOIN is what I want. How do I know what table is on the left and right? Does r.value mean Right Table's Value? – SherwoodPro Nov 08 '13 at 10:57
  • 2
    @SherwoodPro: "Does r.value mean Right Table's Value?" Correct. In the examples posted, you declare the right table as 'r' -- this can be anything you want really, the 'l' (lower-case L) and 'r' are just for simplicities sake. – Stephen Lake Nov 08 '13 at 11:28