look for links between tables poorly connected (not sure of the formulation)
===

:
 

 I have to say that i am far to be an expert in php and mysql, so i did not know how to formulate my question, it explains the vagueness of the title.
I have two tables badly written but i am not allowed to change them. The only thing that they share is a field called url, no primary key, index...
Here is their structure

Shareholder       ||   tags

url1   value1      ||      url1 tag1 tag2 tag350 tag 400

url2   value2      ||     url2 tag8 tag 400 tag 350

url3   value1      ||   url3 tag2 tag1

url 4 value1       ||    url4 tag5 tag 600

url5 value2        ||   url5 tag 60tag8


From these tables i have to find the average number of tags by shareholder, and the shareholders are, for example, value1, value2.  
The expected ouput would be, for example, that shareholder1 (value1)  

It was rather esay to count the number of tags by url with a temporary arrey. Then i think that i have to add these values to a related shareholder, but i'm lost, i do not know how to do and even how to formalize this in php/sql problems.
I have made an attempt which has naturally failed, i paste it below nevertheless. 
Can someone help me ?

    $sqlprim = "SELECT DISTINCT content FROM shareholder";
    $deletedtermsprim = mysql_query ($sqlprim);

    while ($rowprim = mysql_fetch_array($deletedtermsprim))
    {
	$tagsbyshareholder = array();
	foreach ($rowprim as $keys1 => $values1)
	{
		$sql = "SELECT url FROM shareholder WHERE content like \"$values1\"";
		$output1 = mysql_query($sql);
		$deletedterms = array("tags", "[", "]", ":");
	while ($row = mysql_fetch_array($output1)) {
		foreach ($row as $columnName => $columnUrl) {
			$sql2 = "SELECT content
    FROM tags WHERE url like\"$columnUrl\"";
	$deletedterms = mysql_query($sql2);
	while ($row2 = mysql_fetch_array($deletedterms)) 
	{
		foreach ($row2 as $keys2 => $columntags); 
		{
			$preparedtags = str_replace($deletedterms, "", $columntags);
			$temporaryarray = explode(" ", $preparedtags);
			$temporaryarray = array_values(array_filter($temporaryarray));
			$countedtagsbyurl = count($temporaryarray);
			array_push($tagsbyshareholder, $countedtagsbyurl);
			}
	}
	}
    }

    }
    foreach ($tagsbyshareholder as $countitems => $numberoftagsbyshareholder);
    			echo $values1. ";". $numberoftagsbyshareholder;
    			echo "<br />";
    }
