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

问题描述 投票:0回答:1

我试图计算一个值(例如:第一位)出现在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             |    

正如你在dance_placement1中合并之前所看到的那样,“第一名”和“第二名”只出现一次,但合并之后会出现3次。

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

在dancer_placement3中,第3位只出现一次,但合并后出现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";
php mysql mysqli jointable
1个回答
0
投票

而不是加入使用联盟,如下所示:

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
© www.soinside.com 2019 - 2024. All rights reserved.