我试图计算一个值(例如:1st place)在3个不同表的一列中出现的次数。
我按id合并4个表以获得所需的列,然后执行一个数组以获得所有值,然后使用array\u count\u values找出值在表中的次数。
它在很大程度上满足了我的要求,只是答案增加了一倍。当实际值只出现1次时,一旦我合并了表,每个值显示的都比它应该显示的多。
在我合并它们之前,这些是我的table
表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 |
如您所见,在舞者位置1中合并之前,“第一名”和“第二名”只出现一次,但合并后出现3次。
在舞者位置中,第1、第2和第4出现一次,但合并后各出现2次。
在舞者位置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";
1条答案
按热度按时间mctunoxg1#
使用union代替join,如下所示: