Jump to content

Query to get item not assigned to a user


HDFilmMaker2112

Recommended Posts

I'm trying to write a query to get an item not assigned to user. Would this be anywhere near close:

 

SELECT $tbl_name4.item_id FROM $tbl_name4 JOIN $tbl_name2 ON ($tbl_name2.credit=$tbl_name4.position AND $tbl_name2.film_number=$tbl_name4.film_number) WHERE $tbl_name4.item_id IS NULL;

 

My table structure is as such:

 

$tbl_name4="credits_listing";

id | cost | position | item_id | quantity | film_number

 

$tbl_name2="donors_credits";

id | credit | film_number | donor_id

 

 

The only issue I can see with the query I have above is how to I select the specific user to choose the data from? I do have donor_id value available from a previous query. Should I add this value as a second WHERE condition?

Updated Query/PHP:

 

I know this isn't the php forum, but I'm including my php so I can show what the query results will be used for.

 

$tbl_name has donor_id and username

$tbl_name2 has item_id and donor_id

$tbl_name4 has item_id and the item details

 

<?php
session_start();
$myusername=$_SESSION['myusername2'];
$mypassword=$_SESSION['mypassword2'];

require_once 'db_select.php';
$credit=$_GET['credit'];
$credit=explode('-',$credit);
$credit2=$credit[0];
$id=$_GET['id'];

$sql_films="
SELECT $tbl_name4.position,$tbl_name4.film_number 
FROM $tbl_name4 
LEFT JOIN $tbl_name2 
ON $tbl_name4.item_id=$tbl_name2.item_id 
LEFT JOIN $tbl_name 
ON $tbl_name.donor_id=$tbl_name2.donor_id 
AND $tbl_name.donor_id='$id' 
WHERE $tbl_name4.item_id IS NULL";
$result_films=mysql_query($sql_films) or die(mysql_error());  
  
  $select.='<select name="new_credit">'."\n";
  $select.='<option value="">Select Credit</option>'."\n";                
  while($row_films=mysql_fetch_array($result_films)){
  extract($row_films);
       $select.='<option value="'.$position.'-'.$film_number.'" >'.ucwords($position).' - Film # '.$film_number.'</option>'."\n";
  }
$select.='</select> ';

echo $select;
?>

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.