this is my 1st post so excuse me if my question is a bit weird. I work on a test project for an android application that send some data to a server. The java part is already done but I don't have much knowledge on serverside things and databases.So I need to find a way to compare two tables. A small one and a much bigger. For example,the small one is a list of a company's employees and the bigger is a database of companies and their employees. I need to make an array with the counts of the similar rows from the comparison of the two tables. I know my English are a bit rusty so I will try to be more clear:
company x:
|id |emp_name|age|
|2314|bob |33 |
|2314|sam |45 |
|2314|paul |28 |
companies database:
|id |emp_name|age|
|1342|nick |33 |
|1342|helen |38 |
|1342|john |52 |
|... |
|6742|bob |33 |
|6742|julia |36 |
|6742|paul |28 |
|... |
|7654|sam |45 |
|6742|bob |33 |
|6742|paul |28 |
|... |
so for the above example I need to make an array like this:
|0|2|3|.....|
I don't care about the company id, just for names and age.
May be this will help
$arr=array(0=> array("id"=>'1', "emp_name"=>"bob", "age"=>33),1=> array("id"=>'2', "emp_name"=>"sam", "age"=>45),2=> array("id"=>'3', "emp_name"=>"paul", "age"=> 28));
$company=array(0=> array("id"=>'1', "emp_name"=>"bob", "age"=>33),1=> array("id"=>'2', "emp_name"=>"samd", "age"=>45),2=> array("id"=>'3', "emp_name"=>"paul", "age"=> 28),3=> array("id"=>'1', "emp_name"=>"bobe", "age"=>33),4=> array("id"=>'2', "emp_name"=>"same", "age"=>45),5=> array("id"=>'3', "emp_name"=>"paul", "age"=> 28),6=> array("id"=>'1', "emp_name"=>"bob", "age"=>33),7=> array("id"=>'2', "emp_name"=>"sam", "age"=>45),8=> array("id"=>'3', "emp_name"=>"paul", "age"=> 28));
$result=array();
foreach($arr as $ar1){
$i=0;
foreach($ar1 as $ar2key=>$ar2val){
foreach($company as $comp1){
foreach($comp1 as $comp2key=>$comp2val){
if($ar1["emp_name"]==$comp1[$comp2key]){
$i+=1;
$result[$comp2val]=$i;
}
}
}
}
}
print_r($result);
//$result=array_values($result);
//print_r($result);
Simply use an aggregate SQL query to count the matching companies between both tables. Then, have PHP run the query using its MySQL PDO or other database API into an associative array and/or designate a separate array and append query counts into it:
SQL Query
SELECT s.emp_name, s.age
COUNT(*) As emp_count
FROM smalltable s
INNER JOIN largetable l
ON s.emp_name = l.emp_name
AND s.age = l.age
GROUP BY s.emp_name, s.age
PHP Script
try {
// OPEN CONNECTION
$dbh = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT s.emp_name, s.age
COUNT(*) As emp_count
FROM smalltable s
INNER JOIN largetable l
ON s.emp_name = l.emp_name
AND s.age = l.age
GROUP BY s.emp_name, s.age";
// QUERY AND FETCH RESULTS
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage();
exit;
}
$values = [];
// ITERATE THROUGH ASSOCIATIVE ARRAY, $row
while($row = $STH->fetch()) {
$values[] = $row['emp_count']; // APPEND emp_count TO $values ARRAY
echo $row['emp_count']; // ECHO emp_count
}
// CLOSE CONNECTION
$dbh = null;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.