简体   繁体   English

如何根据特定的键值通过比较两个表(在数据库中)获得多个计数

[英]How to get multiple counts from the comparison of tow tables(in a database) based on specific key values

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. 我正在为一个将一些数据发送到服务器的android应用程序的测试项目工作。 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. Java部分已经完成了,但是我对服务器端的东西和数据库了解不多。所以我需要找到一种比较两个表的方法。 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: 公司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. 我不在乎公司ID,只在乎姓名和年龄。

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. 只需使用聚合SQL查询来计算两个表之间的匹配公司。 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: 然后,让PHP使用其MySQL PDO或其他数据库API将查询运行到关联数组中和/或指定一个单独的数组,并将查询计数附加到其中:

SQL Query 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

PHP Script PHP脚本

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM