显示MySQL数据库中的唯一值

时间:2016-02-22 15:09:14

标签: php mysql sql mysqli

我报告的问题更简洁一些。 (删除其他)

我有一个我建立的时间报告系统,我正在尝试创建一个遵守类型报告。

基本上我正在尝试执行以下操作:

  1. 根据where子句和字段practice = ccv&,收集我数据库中的所有唯一名称($ fn,$ ln)。年= 2016年(例如)
  2. 然后我还想收集另一个名单($ fn,$ ln),基于practice = ccv& year = 2016和其他where子句= 8
  3. 这将为我提供2个可以比较的DISTINCT列表,差异将是第8周的优秀时间卡。
  4. 从那里,我需要显示提交的所有名称(来自上面的#3)以及未完成的名单(通过比较上面的#1和#2)

    到目前为止,我可以获得名单。但是,我需要使用数据库中的每个人(Region,Timestamp等)显示其他数据。

    期望输出将是这样的:
    提交
    EMEA
    约翰J Wally R
    北美
    戴维斯D
    鲍勃C
    约翰Y
    杰出
    EMEA
    凯尔D
    北美
    Cory T
    休R 约翰J 的 LATAM
    等....

    到目前为止,这就是我所拥有的......我确信我会以错误的方式解决这个问题。

    <?php
    $con = mysqli_connect("localhost","xxx","xxx!","xxx");                  
    
    //Get distinct names from practice
    if ($con) {
    $SQL = "SELECT DISTINCT fn,ln FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";   
    }
    
    $result = mysqli_query($con,$SQL);
    
    if (!$result) die('Couldn\'t fetch records'); 
    
    while ( $db_field = mysqli_fetch_assoc($result) ) {
    $name[] = trim($db_field['fn']. " " .$db_field['ln']);
    }
    
    
    //Get distinct names for current week practice
    if ($con) {
    $SQL2 = "SELECT DISTINCT fn,ln FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln"; 
    }
    
    $result2 = mysqli_query($con,$SQL2);
    
    if (!$result2) die('Couldn\'t fetch records Again'); 
    
    while ( $db_field = mysqli_fetch_assoc($result2) ) {
    
    $name2[] = trim($db_field['fn']. " " .$db_field['ln']);
    }
    
    //$SQL3 = "SELECT fn,ln,week_start_date,region FROM $table WHERE fn IN ( '".implode("', '", $differences)."' )");
    
    mysqli_close($con);
    ?>
    
    <TABLE><TR><TD>
    <B>All Names</B><BR>
    <?php
    foreach ( $name as $item ) {
        echo $item . "<br/>";
    }
    ?>
    </TD>
    <TD>
    <B>Names for Week 8</B><BR>
    <?php
    foreach ( $name2 as $item2 ) {
        echo $item2 . "<br/>";
    }
    ?>
    </TD>
    <TD>
    <B>Outstanding</B><BR>
    <?php
    $results = array_diff($name, $name2);
    
    foreach($results as $val) {
        echo $val ." - ".$val2."<BR>";          
    }
    ?>
    
    </TD>
    </TR>
    </TABLE>
    

1 个答案:

答案 0 :(得分:1)

我认为你应该在MySQL级别解决这个问题。

// All names with all data
$SQL = "SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";

// Names for specific week with all data
$SQL = "SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes' ORDER BY region,fn,ln";

// Outstanding rows with all data
$SQL = "SELECT * FROM (SELECT DISTINCT *, CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND practice='$practice' AND archived!='yes') AS `subquery` WHERE `name` NOT IN (SELECT DISTINCT CONCAT(fn,' ',ln) AS `name` FROM $table WHERE year='$year' AND week_num='$week' AND practice='$practice' AND archived!='yes') ORDER BY region,fn,ln";

这样您就不需要在PHP级别对数据进行排序和处理。无法测试这个,因为我没有数据库环境,但它应该工作,可能需要一些小的改动。