php - 为什么当我合并 mysql 数据库中的表时,值是重复的?

标签 php mysql mysqli join

我正在尝试计算某个值(例如:第一名)在 3 个不同表的列中出现的次数。

我按 ID 合并 4 个表来获取我需要的列,然后创建一个数组来获取所有值,然后使用 array_count_values 找出该值在表中出现的次数。

它在很大程度上满足了我的要求,只是答案加倍了。当实际上该值只出现 1 次时,一旦我合并表格,每个值显示的次数就超过了应有的次数。

这些是我合并之前的表格

表1

| dancer_id | dancer_placement1|
|    1      |   2nd Place      |
|    1      |   1st Place      |
|    3      |   4th Place      |

表2

| dancer_id | dancer_placement3|
|    1      |   4th Place      |
|    1      |   1st Place      |
|    3      |   3rd Place      |
|    1      |   2nd Place      |

表3

| dancer_id | dancer_placement3|
|    2      |   6th Place      |
|    3      |   1st Place      |
|    1      |   3rd Place      |

当我合并它们时,它看起来像这样

| dancer_id | dancer_placement1|  dancer_placement2|  dancer_placement3|
|    1      |   2nd Place      |  4th Place        |  3rd Place        |    
|    1      |   1st Place      |  4th Place        |  3rd Place        |    
|    1      |   2nd Place      |  1st Place        |  3rd Place        |    
|    1      |   1st Place      |  1st Place        |  3rd Place        |
|    1      |   2nd Place      |  2nd Place        |  3rd Place        |
|    1      |   1st Place      |  2nd Place        |  3rd Place        |

它实际上应该是这样的:

| dancer_id | dancer_placement1|  dancer_placement2|  dancer_placement3|
|    1      |   2nd Place      |  4th Place        |  3rd Place        |    
|    1      |   1st Place      |  1st Place        |  null             |    
|    1      |   null           |  2nd Place        |  null             |    

正如您在 dancer_placement1 合并之前看到的,“第一名”和“第二名”仅出现一次,但合并后出现 3 次。

在 dancer_placement2 中,第 1、第 2、第 4 出现一次,但合并后各出现 2 次。

在 dancer_placement3 中,第三名仅出现一次,但合并后出现 6 次。

我认为我的 sql 可能有问题,因为其他一切都正常。

这是我的代码:

//GET ID 

if(isset($_GET['id'])) {
    $childId=$_GET['id']; 
//I MERGE MY TABLES
    $chartsql = <<<_SQL
SELECT
  dancers.id, 
  mark_cards1.dancer_placement1, 
  mark_cards2.dancer_placement2, 
  mark_cards3.dancer_placement3 
FROM dancers 
LEFT JOIN mark_cards1 ON mark_cards1.dancer_id1 = dancers.id
LEFT JOIN mark_cards2 ON mark_cards2.dancer_id2 = dancers.id 
LEFT JOIN mark_cards3 ON mark_cards3.dancer_id3 = dancers.id 
WHERE dancers.id = '$childId'
_SQL;

$pieChartRes = mysqli_query($con,$chartsql);

 //BELOEW CODE WORKS PERFECTLY FINE

     // creating an array to find values
    while ($pieChartRow=mysqli_fetch_array($pieChartRes)){
        $first[] = $pieChartRow['dancer_placement1'];
        $second[] = $pieChartRow["dancer_placement2"];
        $third[]= $pieChartRow["dancer_placement3"];

    }

   // COUNTING THE AMOUNT OF TIMES A SPECIFIC VALUE APPEARS

    $tmp = array_count_values($first);
    $count_first = $tmp["1st place"];
    $count_second = $tmp["2nd place"];
    $count_third = $tmp["3rd place"];

    $tmp2 = array_count_values($second);
    $count_first2 = $tmp2["1st place"];
    $count_second2 = $tmp2["2nd place"];
    $count_third2 = $tmp2["3rd place"];

    $tmp3 = array_count_values($third);
    $count_first3 = $tmp3["1st place"];
    $count_second3 = $tmp3["2nd place"];
    $count_third3 = $tmp3["3rd place"];

  //PRINTING NUMBER OF VALUES

    echo "number of 1st place in dancerplacement1: $count_first";
    echo "number of 1st place in dancerplacement2: $count_first2";
    echo "number of 1st place in dancerplacement3: $count_first3";

    echo "number of 2nd place in dancerplacement1: $count_second";
    echo "number of 2nd place in dancerplacement2: $count_second2";
    echo "number of 2nd place in dancerplacement3: $count_second3";

    echo "number of 3rd place in dancerplacement1: $count_third";
    echo "number of 3rd place in dancerplacement2: $count_third2";
    echo "number of 3rd place in dancerplacement3: $count_third3";

// GETTING TOTALS

    $first_total = $count_first + $count_first2 + $count_first3;
    $second_total = $count_second + $count_second2 + $count_second3;
    $third_total = $count_third + $count_third2 + $count_third3;
    echo "total for first place = $first_total";
    echo "total for second place = $second_total";
    echo "total for third place = $third_total";

最佳答案

使用 union all 代替 Join,如下所示:

select dancerid,dancer_placement1,null as dancer_placement2,null as dancer_placement3
from table1 where dancerid=1
union all
select dancerid,null,dancer_placement2,null
from table2 where dancerid=1
union all
select dancerid,null,null,dancer_placement3
from table3 where dancerid=1

关于php - 为什么当我合并 mysql 数据库中的表时,值是重复的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52379059/

相关文章:

MySQL - 在同一个表中计数

php - 为什么我会收到这些错误? php 和 mysql

PHP 检查数据库中的项目

javascript - jQuery 验证中的正则表达式 - 需要一个包含 5 位数字和 1 个字母的数字(字母不区分大小写)

PHP 查询字符串限制

mysql - 使用哪种字符编码

mysql - 如何通过两个 IN 变量进行排序

php - 我收到语法错误拒绝访问用户 'root' @'localhost'(使用密码 : NO)

javascript - Symfony2 Bundles 资源文件丢失

php - 在两个页面请求之间保留 MySQL 事务 (PDO)?