为什么当我在 mysql 数据库中合并表时,值是重复的?
Why when I merge tables in mysql database the values are duplicating?
我正在尝试计算一个值(例如:第一名)在 3 个不同的 table 列中出现的次数。
我通过ID合并4个table得到我需要的列,然后做一个数组得到所有的值,然后我用array_count_values求值的次数在 table.
除了答案加倍之外,它大部分都符合我的要求。当实际上该值只出现 1 次时,一旦我合并 tables,每个值都显示得比它应该的多。
这些是我合并前的 table
Table 1
| dancer_id | dancer_placement1|
| 1 | 2nd Place |
| 1 | 1st Place |
| 3 | 4th Place |
table 2
| dancer_id | dancer_placement3|
| 1 | 4th Place |
| 1 | 1st Place |
| 3 | 3rd Place |
| 1 | 2nd Place |
table 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 1st, 2nd, 4th出现一次,合并后各出现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";
使用 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
我正在尝试计算一个值(例如:第一名)在 3 个不同的 table 列中出现的次数。
我通过ID合并4个table得到我需要的列,然后做一个数组得到所有的值,然后我用array_count_values求值的次数在 table.
除了答案加倍之外,它大部分都符合我的要求。当实际上该值只出现 1 次时,一旦我合并 tables,每个值都显示得比它应该的多。
这些是我合并前的 table
Table 1
| dancer_id | dancer_placement1|
| 1 | 2nd Place |
| 1 | 1st Place |
| 3 | 4th Place |
table 2
| dancer_id | dancer_placement3|
| 1 | 4th Place |
| 1 | 1st Place |
| 3 | 3rd Place |
| 1 | 2nd Place |
table 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 1st, 2nd, 4th出现一次,合并后各出现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";
使用 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