Question MySQL compair of two lists

Kayot

Well-known member
Joined
Mar 6, 2007
Messages
49
Programming Experience
3-5
I'm not sure if this is possible, but figured what the heck. So here it is;

I've been writing a site in PHP for quite some time now. That is all good and dandy. But recently I've stepped up my efforts to remove unnecessary data. What I wondered is if there is a way to do what the following script does, in SQL only.

PHP:
<?php 
function list1() {
	include("config.php");
	$SQL = "SELECT id FROM tradeskill_recipe;";
	$result = mysql_query($SQL);
	$count = 0;
	while ($db_field = mysql_fetch_assoc($result)) {
		//$db_field['']
		$return[$count] = $db_field['id'];
		$count++;
	}
	mysql_close($db_handle);
	return $return;
}	
		
function list2() {		
	include("config.php");
	$SQL = "SELECT recipe_id 
			FROM tradeskill_recipe_entries
			where iscontainer > 0
			group by recipe_id
			;";
	$result = mysql_query($SQL);
	
	$count = 0;
	while ($db_field = mysql_fetch_assoc($result)) {
		
		$return[$count] = $db_field['recipe_id'];
		$count++;

	}

	mysql_close($db_handle);
	return $return;
}

function check() {
	$list1 = list1();
	$list2 = list2();
	
	//clean list
	for ($i = 0; $i <= count($list2) - 1; $i++) {
		$check = array_search($list2[$i], $list1);
		if ($check>0) {
			$list1[$check] = "";
		}
	}

	for ($i = 0; $i <= count($list1) - 1; $i++) {
		if ($list1[$i]!="") {
			Print $list1[$i] . "<br>";
		}
	}
}
check();
?>

Basically, what the above does, is it forms two lists of numbers. The second list is compared to the first list and it empties the list till only unique values are left. The real world example is, there is an entry in list 1 that has multiple entries in list two. If there aren't any entries in list two, then it will show up at the end of the script. Other wise if it does have sub entries, it won't show up. And that is done on hundreds of thousands of entries. Using the above script it takes a whole eight seconds. But if it can be done in just SQL I'd like to do that instead.

I also wondered, how do I use a join and what is it good for? This site kind of explains it, but I'm not sure of a good way to use it. I think it might have something to do with my question, which is why I'm bringing it up.

P.S. I just relized I put this in the wrong section, could an OP please move it to the MySQL section?
 
Last edited:
Back
Top